gpt4 book ai didi

r - 设置ggplot2标签背景色

转载 作者:行者123 更新时间:2023-12-02 09:23:49 27 4
gpt4 key购买 nike

我有这个条形图

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))

我希望标签是白色背景和黑色字体,但是当我添加 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)),colour = "black",
position= position_dodge(width=1))

另请注意

如果我将 fill="white" 移到 aes() 之外,那么标签不会在条形图上方,而是相互堆叠。即它抵消了 position=position_dodge(width=1) 的效果,我需要在条上放置标签

谢谢。

最佳答案

做两个改变:

  1. fill = factor(day) 移动到 geom_bar 内的 aes()
  2. geom_label 中设置 group = factor(day)

如图所示:

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))

enter image description here

关于r - 设置ggplot2标签背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39514394/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com