[R] barplot question

Rui Barradas ruipbarradas at sapo.pt
Mon Jul 30 22:49:39 CEST 2012


Hello,

Try the following.


plants_herbs_input_top10 <- structure(list(total = c(28L, 17L, 11L, 6L, 
6L, 5L, 4L, 3L, 3L,
2L), young = c(29L, 22L, 15L, 8L, 5L, 3L, 2L, 2L, 2L, 2L), old = c(36L,
11L, 11L, 8L, 6L, 5L, 4L, 3L, 2L, 2L)), .Names = c("total", "young",
"old"), class = "data.frame", row.names = c(NA, -10L))

# Keep the return value, we'll need it later
# to know where to place the bars names
bp <- barplot(data.matrix(plants_herbs_input_top10),
     width = 1,
     ylim = c(0,50),
     xaxt = "n",
       space = c(0.2, 1.0),   #------> this has changed
     ylab = "Relative frequencies (%)",
     beside = TRUE,
       col = rep(c("black", "chartreuse1", "chartreuse4"), each=10),
     cex.names = 0.8)       #------> this has changed
#------> line below commented out
#    names.arg = rep(as.character(1), 3))
text(as.vector(bp), y = -1, labels = rep(1:10, 3), xpd = TRUE)

legend.width <- max(strwidth(c("Total", "Young secondary forest","Old 
secondary forest")))

legend("topright",
     c("Total", "Young secondary forest","Old secondary forest"),
     cex=0.9, pt.cex=1, pch=15, y.intersp=0.8,
     text.width = legend.width/2,      #------> this was added
     col=c("black","chartreuse1","chartreuse4"))


Hope this helps,

Rui Barradas

Em 30-07-2012 11:46, Michael Eisenring escreveu:
>   Dear r-help members.
>
> I would like to:
>
> a) control the margin around my legend box. Unfortunately I did not find an appropriate command under "?legend". The margin around the actual legend is way too wide. There is a lot of unnecessary "empty space" on the right side.
>
> b) increase the width of the individual barplots. I saw that this can be obtained with the command "width" and "xlim".However, since I have 3 Barplots next to each other "beside=T", I could not figure it out how to do it (2 Barplots are disappearing)
>
> c) I wanted to label each bar ("names.arg") unfortunately nothing happens if I use this command (in my code i just used 1:10 for the names, in the original plot I would replace the numbers with real names)
>
> Thank you very much:
>
> Input data (dput)
>
> structure(list(total = c(28L, 17L, 11L, 6L, 6L, 5L, 4L, 3L, 3L,
> 2L), young = c(29L, 22L, 15L, 8L, 5L, 3L, 2L, 2L, 2L, 2L), old = c(36L,
> 11L, 11L, 8L, 6L, 5L, 4L, 3L, 2L, 2L)), .Names = c("total", "young",
> "old"), class = "data.frame", row.names = c(NA, -10L))
>
>
>
>
> R-code:
> barplot(as.matrix(plants_herbs_input_top10),xaxt="n",
>          space=c(0.5,12),ylab= "Relative frequencies (%)",beside=TRUE,
>          col=c(rep("black",10),rep("chartreuse1",10),rep("chartreuse4",10)),ylim=c(0,50),cex.names=0.8,names.arg=c("1", "2", "3","4","5","6","7","8","9","10","1", "2", "3","4","5","6","7","8","9","10","1", "2", "3","4","5","6","7","8","9","10"))
>          legend("topright",c("Total","Young secondary forest","Old secondary forest"),cex=0.9,pt.cex=1,y.intersp=0.4,pch=15,col=c("black","chartreuse1","chartreuse4"))
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list