简体   繁体   中英

How can I create a grouped bar plot in R using the ggplot and aes functions?

I'm trying to create a grouped bar plot from the following data (returned from a ddply function) where the x-axis has all 4 CWD variables (for each of the 2 sites) and the y axis is the mean.

My code is:

library(plyr)
library(reshape2)
library(ggplot2)

 ddply(data, c("Site","Plot","Cover"), summarise, mean=mean(Height), sd=sd(Height), 
    sem=sd(Height)/sqrt(length(Height)))

BranchSize <- ddply(data, c("Site","CWD"), summarise, mean=mean(Volume),
    sd=sd(Volume), sem=sd(Volume)/sqrt(length(Volume)))

and it returns this table. Is this table a data frame already or do I need to make it one in order to work with it?

  Site    CWD        mean         sd        sem
1 High   Bark    975.7273   2603.077   554.9780
2 High Branch  36827.7735 107668.064 13056.6706
3 High   Cage 116041.4286 154934.888 58559.8832
4 High    Log  73463.3636 121054.372 25808.8788
5  Low   Bark     40.0000         NA         NA
6  Low Branch   1323.8280   2304.571   595.0377
7  Low   Cage    101.5000         NA         NA
8  Low    Log 102600.0000         NA         NA

Then using this code:

limits <- aes(ymax = BranchSize$mean + BranchSize&se, 
    ymin=BranchSize$mean - BranchSize&se)
CWDVol<-ggplot(data = BranchSize, 
    aes(x = factor(CWD), y = mean, fill = factor(Site)))
CWDVol

When I run this command, my plot appears but there are no bars.

Then when I run this:

CWDVol + geom_bar(stat = "identity", position_dodge(0.9)) + 
    geom_errorbar(limits, position = position_dodge(0.9), width = 0.25) + 
    labs(x = "CWD Type", y = "Average Volume") + 
    ggtitle("Average CWD Size in each Site") + 

    scale_fill_discrete(name = "Site")

I keep getting this error: "Error: Mapping must be created by aes() or aes_() "

Any tips would be much appreciated.

tl;dr You left out the name of the position argument, so geom_bar assumed that position_dodge(0.9) was the mapping argument. Once you fix that everything seems to work nicely.

BranchSize <- read.table(header=TRUE,text="
Site    CWD        mean         sd        sem
High   Bark    975.7273   2603.077   554.9780
High Branch  36827.7735 107668.064 13056.6706
High   Cage 116041.4286 154934.888 58559.8832
High    Log  73463.3636 121054.372 25808.8788
Low   Bark     40.0000         NA         NA
Low Branch   1323.8280   2304.571   595.0377
Low   Cage    101.5000         NA         NA
Low    Log 102600.0000         NA         NA")

library(ggplot2)
limits <- aes(ymax=mean+sem, ymin=mean-sem)
CWDVol <- ggplot(data=BranchSize,
                 aes(x=factor(CWD),y=mean,fill=factor(Site)))
CWDVol + geom_bar(stat="identity",position=position_dodge(0.9))+
   geom_errorbar(limits, position = position_dodge(0.9), width = 0.25) + 
   labs(x = "CWD Type", y = "Average Volume") + 
   ggtitle("Average CWD Size in each Site")+
   scale_fill_discrete(name = "Site")+
   scale_y_log10()

Some other suggestions:

  • don't use $ within mappings, it will just make trouble
  • maybe use +/- 2 SEM (or 1.96 SEM) for confidence intervals? (All your viewers will probably be trying to do this transformation in their heads otherwise.)

The results of the code are as follows 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM