View Full Version : Need help with R(Barplot)


naivesincerity
11-08-2006, 12:04 PM
So, I'm trying to barplot, and it won't show up, how do I correct this?

BioStatMatt
11-08-2006, 12:59 PM
It will be difficult to determine what you are doing wrong without seeing the command you used and the output. Do you get errors?

naivesincerity
11-08-2006, 01:11 PM
>barplot(Election8387929701,space=c(0,0.5),beside=T RUE,col=c("red","blue","yellow"),legend.text=c("Labour","Conservative","Liberal Democrats"),ylim=c(0,50),names.arg=year,ylab="Percentage of vote")

naivesincerity
11-08-2006, 01:13 PM
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,Ele ction92,Election97,Election01)

naivesincerity
11-08-2006, 01:18 PM
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

naivesincerity
11-08-2006, 01:42 PM
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
11-08-2006, 06:42 PM
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,Ele ction92,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,Ele ction92,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

naivesincerity
11-09-2006, 04:13 AM
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