[R] barplot question

Rui Barradas ruipbarradas at sapo.pt
Tue Jul 31 11:25:34 CEST 2012


Hello,

Ok, final retouches.

1. Divide the par() call:

oldpar <- par(mar=c(9, 4.1, 4.1, 2.1))
oldfont <- par(font=3)

Then, just before legend()

par(oldfont)   # back to normal font

2. Modify the text() call to

text(as.vector(bp), y = -6.5, cex=0.7,
     pos = 2,
     offset = -0.25,
     ...etc...

(See the meaning in the help page for text().)

I think this does it. Good luck.

Rui Barradas

Em 31-07-2012 09:43, Michael Eisenring escreveu:
> Hi,
> thank you very much, looks awesome!
> I changed a few minor things. Right now I have a some last questions (i sent them also to R help)
>
> 1: I changed the font style of the plant names (along x-axis, under the bars)  to "italic" . How can I avoid that the font in the legend also changes to "italic"?
> 2. I would like to place the plant names (along x-axis, under the bars) in a way that :
> a) the first plant name is under the first bar (now the first name is too much to the right). If I change the x value under "text" all names are shifted to this one x-coordinate.
> b)the plant names should start all on the same "height" (e.g. all exactly on y=-6.5. Now they are misplaced
>
> Any ideas?
>
> Thank you very much
> Michi
>
>
> 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
> 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),
>                ylab = "Relative frequencies (%)",
>                beside = TRUE,
>                col = rep(c("black", "chartreuse1", "chartreuse4"), each=10),
>                cex.names = 0.8)
> par(mar=c(9, 4.1, 4.1, 2.1),font=3)
>
> #    names.arg = rep(as.character(1), 3))
> text(as.vector(bp),y=-6.5, cex=0.7,labels = c("Oplismenus sp.","Isachne mauritiana","Piper capense","Asplenium aethiopicum","Psychotria sp (seedlings)","Elatostema monticolum","Landolphia buchananii","Blotiella sp.","Asplenium elliottii","Hypoestes triflora","Isachne mauritiana","Oplismenus sp","Piper capense","Landolphia buchananii","Asplenium aethiopicum","Blotiella sp.","Carex chlorosaccus","Asplenium elliottii","Asplenium friesiorum","Polystichum sp.","Oplismenus sp","Psychotria sp (seedlings)","Elatostema monticolum","Asplenium aethiopicum","Hypoestes triflora","Piper capense","Asplenium elliottii","Blotiella sp.","Isoglossa substrobilina","Polystichum sp."),srt = 50,xpd = TRUE)
>
> legend.width <- max(strwidth(c("Total", "Young secondary forest","Old secondary forest")))
>
> legend(23,54,
>         c("Total", "Young secondary forest","Old secondary forest"),
>         cex=1, pt.cex=1.7, pch=15, y.intersp=0.4,bty="n",
>         text.width = legend.width/2.5,      #------> legendbox : text ratio
>         col=c("black","chartreuse1","chartreuse4"))
>
>
>
>
>



More information about the R-help mailing list