设置ggplot2标签背景颜色 [英] Set ggplot2 label background color
问题描述
我有这个棒图
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)
的效果,我需要在酒吧上的标签
进行两项更改:
- 在
geom_bar内将
fill = factor(day)
移至aes()
- 在
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 fontgroup = 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 theaes()
then the labels are not over the bars but stacked on one another. i.e. it negates the effect ofposition=position_dodge(width=1)
and I need the labels over the barsThank you.
解决方案Make two changes:
- Move
fill = factor(day)
intoaes()
within thegeom_bar
- Set
group = factor(day)
in yourgeom_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屋!
- Move