设置ggplot2标签背景颜色 [英] Set ggplot2 label background color

查看:1587
本文介绍了设置ggplot2标签背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个棒图

  group = c(A,A,B,B )
value = c(25,-75,-40,-76)
day = c(1,2,1,2)
dat = data.frame(group = group ,value = value,day = day)
dat
ggplot(data = dat,aes(x = factor(group),y = value,fill = factor(day)))+
geom_bar(stat =identity,position =dodge)+
geom_label(aes(label = round(value,0),fill =white),
color =black,position = position_dodge(width = 1))

我希望这些标签是黑色字体的白色背景但是当我添加 fill =white时,绘图不正确。标签没有白底黑字体。



注意这里没有 fill =white情节看起来不错。我只是想改变标签背景和字体

  group = c(A,A,B, B)
value = c(25,-75,-40,-76)
day = c(1,2,1,2)
dat = data.frame(group = group = value = value,day = day)

ggplot(data = dat,aes(x = factor(group),y = value,fill = factor(day)))+
geom_bar(stat =identity,position =dodge)+
geom_label(aes(label = round(value,0)),color =black,
position = position_dodge(width = 1 ))

另请注意



如果我在 aes()之外移动 fill =white,那么标签还没有结束酒吧,但堆叠在一起。即它否定了 position = position_dodge(width = 1)的效果,我需要在酒吧上的标签




解决方案

进行两项更改:


  1. geom_bar内将 fill = factor(day)移至 aes()

  2. geom_label group = factor(day) c $ c


    如下所示:

      ggplot(data = dat,aes(x = factor(group),y = value))+ 
    geom_bar(aes(fill = factor(day)),stat =identity,position = (aes(label = round(value,0),group = factor(day)),color =black,position = position_dodge(width = 1))


    I have this bar plot

    group = c("A","A","B","B")
    value = c(25,-75,-40,-76)
    day = c(1,2,1,2)
    dat = data.frame(group = group , value = value, day = day)
    dat
    ggplot(data=dat, aes(x=factor(group), y=value, fill=factor(day))) +
       geom_bar( stat="identity", position="dodge")+
       geom_label(aes(label =round(value,0),fill="white"),
       colour = "black", position= position_dodge(width=1))
    

    I'd like the lables to be white backgroud with black font but when I add fill="white" the plot is incorrect. The labels do not have white background with black font.

    notice here without the fill="white" the plot looks good. I just want to change label background and font

    group = c("A","A","B","B")
    value = c(25,-75,-40,-76)
    day = c(1,2,1,2)
    dat = data.frame(group = group , value = value, day = day)
    
    ggplot(data=dat, aes(x=factor(group), y=value, fill=factor(day))) +
      geom_bar( stat="identity", position="dodge")+
      geom_label(aes(label =round(value,0)),colour = "black", 
        position= position_dodge(width=1))
    

    ALSO NOTE

    If I move fill="white" outside the aes() then the labels are not over the bars but stacked on one another. i.e. it negates the effect of position=position_dodge(width=1) and I need the labels over the bars

    Thank you.

    解决方案

    Make two changes:

    1. Move fill = factor(day) into aes() within the geom_bar
    2. Set group = factor(day) in your geom_label

    As shown here:

    ggplot(data=dat, aes(x=factor(group), y=value)) +
      geom_bar(aes(fill = factor(day)), stat="identity", position="dodge")+
      geom_label(aes(label =round(value,0), group = factor(day)),colour = "black", position= position_dodge(width=1))
    

    这篇关于设置ggplot2标签背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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