Need help with R(Barplot)

#3
>barplot(Election8387929701,space=c(0,0.5),beside=TRUE,col=c("red","blue","yellow"),legend.text=c("Labour","Conservative","Liberal Democrats"),ylim=c(0,50),names.arg=year,ylab="Percentage of vote")
 
Last edited:
#4
Before that was:

> Election01=c(41.41,35.23,19.42)
> Election83=c(26.94,45.98,14.36)
> Election87=c(29.51,46.24,13.58)
> Election92=c(33.93,45.46,19.18)
> Election97=c(43.55,33.69,17.95)

>year=c(1982,1987,1992,1997,2001)
>Election8387929701=rbind(Election83,Election87,Election92,Election97,Election01)
 
#5
But even when it does respond with a '+' sign, as though the plot has been drawn, with no error message, I can't find the plot on the terminal
 
Last edited:
#6
Error in barplot.default(Election9701, space = c(0, 0), beside = TRUE, :
incorrect number of names
In addition: Warning message:
parameter "legend.txt" couldn't be set in high-level plot() function


Got this message
 

BioStatMatt

TS Contributor
#7
try changing the "rbind" to "cbind" like this:

Election01=c(41.41,35.23,19.42)
Election83=c(26.94,45.98,14.36)
Election87=c(29.51,46.24,13.58)
Election92=c(33.93,45.46,19.18)
Election97=c(43.55,33.69,17.95)

year=c(1982,1987,1992,1997,2001)
Election8387929701=cbind(Election83,Election87,Election92,Election97,Election01)

barplot(Election8387929701,space=c(0,0.5),beside= TRUE,col=c("red","blue","yellow"),legend.text=c("Labour","Conservative","Liberal Democrats"),ylim=c(0,50),names.arg=year,ylab="Perc entage of vote")


If you indeed wanted groups of bars according to political party, it looks like you have used names.args and legend.text in the incorrect places. A legend identifies a particular bar within a group, the names.arg argument holds a vector of names for the groups. You also have some funky colors. Try this code:

Election01=c(41.41,35.23,19.42)
Election83=c(26.94,45.98,14.36)
Election87=c(29.51,46.24,13.58)
Election92=c(33.93,45.46,19.18)
Election97=c(43.55,33.69,17.95)

year=c(1982,1987,1992,1997,2001)
Election8387929701=rbind(Election83,Election87,Election92,Election97,Election01)

barplot(Election8387929701,space=c(0,0.5),beside= TRUE,col=c(2,3,4,5,6),names.arg=c("Labour","Conservative","Liberal Democrats"),ylim=c(0,50),legend.text=year,ylab="Perc entage of vote")


Hope this is the fix,

~Matt
 
Last edited:
#8
Thanks very much indeed, I'll give that a shot!

barplot(Election8387929701,space=c(0,0.2),beside= TRUE,col=c("red","blue","yellow"),legend.text=c("Labour","Conservative","Liberal Democrats"),ylim=c(0,70),names.arg=year,ylab="Percentage of vote",xlab="Election year",width=0.5,xlim=c(0,9))

Made do with that in the end, worked nicely after the 'cbind' too
 
Last edited: