将图例添加到“geom_bar”使用ggplot2软件包 [英] Add legend to "geom_bar" using the ggplot2 package

查看:114
本文介绍了将图例添加到“geom_bar”使用ggplot2软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的新手,所以请原谅我的无知。我做了一个伪堆叠的barplot,其中我使用geom_bar在彼此的顶部绘制了4组条。对于三种橡树(QUAG,QUKE,QUCH),有4种健康状况类别(活着,死亡,感染和死亡)。我的代码如下所示:




  x < -  as.data.frame(list (变量= c(QUAG,QUKE,QUCH),alive = c(627,208,109),infected = c(102,27,0),dead = c(133,112,12),sod.dead = c (49,8,0)))

x.plot = ggplot(x,aes(variable,alive))+ geom_bar(fill =gray85)+
geom_bar(aes(变量,死亡),fill =gray65)+
geom_bar(aes(variable,infected),fill =gray38)+
geom_bar(aes(variable,sod.dead),fill =黑色)+
opts(panel.background = theme_rect(fill ='gray100'))
x.plot






现在我想创建一个图例,显示哪种灰度与树木状态有关,即gray65是死树等。我一直在尝试过去一小时,但无法完成它。

解决方案

我看到@Brandon Ber telsen发布了一个很好的答案。我想添加一些代码来解决原始文章中提到的其他细节:


  1. 重新整理数据并将健康状态映射到 fill ,ggplot会自动创建图例。
  2. 我建议使用 scale_fill_manual()以获得原始文章中提到的确切灰色。

  3. theme_bw()是一种方便的功能,可以快速获取黑白看看你的情节。

  4. 因子水平/颜色的绘图顺序可以通过指定所需的顺序来控制 levels 参数 factor()

  5. 这个数据集可能有一些优点。 b



    $ $ $ $ $ b $ library $($)
    library(ggplot2)

    x< - as.data.frame(list(variable = c(QUAG,QUKE,QUCH),
    alive = c(627,208,109) ,感染= c(102,27,0),
    dead = c(133,112,12),sod.dead = c(49,8,0)))

    #用reshape2软件包中的数据将数据转换成长格式。
    dat = melt(x,id.var =variable,variable.name =status)

    头(dat)
    #变量状态值
    #1 QUAG alive 627
    #2 QUKE alive 208
    #3 QUCH alive 109
    #4 QUAG infected 102
    #5 QUKE infected 27
    #6 QUCH infected 0

    #通过手动指定因子中的等级,可以控制相关填充颜色的堆叠顺序
    #。
    dat $ status = factor(as.character(dat $ status),
    levels = c(sod.dead,dead,infected,alive))

    #创建一个命名的字符向量,将因子水平与颜色相关联。
    grays = c(alive =gray85,dead =gray65,infected =gray38,sod.dead =black)

    plot_1 = ggplot(dat,aes( x = variable,y = value,fill = status))+
    theme_bw()+
    geom_bar(position =stack)+
    scale_fill_manual(values = greys)

    ggsave(plot = plot_1,filename =plot_1.png,height = 5,width = 5)

     #您可能还想尝试躲闪的barplot。 
    plot_2 = ggplot(dat,aes(x = variable,y = value,fill = status))+
    theme_bw()+
    geom_bar(position =dodge)+
    scale_fill_manual(values = greys)

    ggsave(plot = plot_2,filename =plot_2.png,height = 4,width = 5)
    pre>


    I am a newbie to R, so please pardon my ignorance. I made a pseudo-stacked barplot in which I drew 4 sets of bars on top of each other using geom_bar. There are 4 health status categories (alive, dead, infected, & sod-dead) for three species of oak trees (QUAG, QUKE, QUCH).

    My code is as follows:


    x <- as.data.frame(list(variable=c("QUAG", "QUKE", "QUCH"), alive = c(627,208,109),  infected = c(102,27,0), dead = c(133,112,12), sod.dead=c(49,8,0)))
    
    x.plot = ggplot(x, aes(variable, alive)) + geom_bar(fill="gray85") + 
      geom_bar(aes(variable,dead), fill="gray65") +
      geom_bar(aes(variable, infected), fill="gray38") +
      geom_bar(aes(variable, sod.dead), fill="black")+
      opts(panel.background = theme_rect(fill='gray100'))
    x.plot
    


    Now I want to make a legend that shows which shade of gray relates to tree status, i.e., "gray65" is "dead trees", etc. I've been trying for the past hour and can't get it to work.

    解决方案

    I see that @Brandon Bertelsen has posted a great answer. I would like to add some code that addresses additional details mentioned in the original post:

    1. After you reshape your data and map health status to fill, ggplot will create the legend automatically.
    2. I suggest using scale_fill_manual() to get the exact grays mentioned in the original post.
    3. theme_bw() is a handy function to quickly get a black and white look to your plot.
    4. The plotting order of factor levels/colors can be controlled by specifying the desired order with the levels argument of factor().
    5. A dodged barplot (instead of stacked) may have some advantages for this data set.


    library(reshape2)
    library(ggplot2)
    
    x <- as.data.frame(list(variable=c("QUAG", "QUKE", "QUCH"), 
                            alive=c(627, 208, 109),  infected=c(102, 27, 0), 
                            dead=c(133, 112, 12), sod.dead=c(49, 8, 0)))
    
    # Put data into 'long form' with melt from the reshape2 package.
    dat = melt(x, id.var="variable", variable.name="status")
    
    head(dat)
    #    variable   status value
    # 1      QUAG    alive   627
    # 2      QUKE    alive   208
    # 3      QUCH    alive   109
    # 4      QUAG infected   102
    # 5      QUKE infected    27
    # 6      QUCH infected     0
    
    # By manually specifying the levels in the factor, you can control
    # the stacking order of the associated fill colors.
    dat$status = factor(as.character(dat$status), 
                        levels=c("sod.dead", "dead", "infected", "alive"))
    
    # Create a named character vector that relates factor levels to colors.
    grays = c(alive="gray85", dead="gray65", infected="gray38", sod.dead="black")
    
    plot_1 = ggplot(dat, aes(x=variable, y=value, fill=status)) +
             theme_bw() +
             geom_bar(position="stack") +
             scale_fill_manual(values=grays)
    
    ggsave(plot=plot_1, filename="plot_1.png", height=5, width=5)
    

    # You may also want to try a dodged barplot.
    plot_2 = ggplot(dat, aes(x=variable, y=value, fill=status)) +
             theme_bw() +
             geom_bar(position="dodge") +
             scale_fill_manual(values=grays)
    
    ggsave(plot=plot_2, filename="plot_2.png", height=4, width=5)
    

    这篇关于将图例添加到“geom_bar”使用ggplot2软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆