r - ggplot : conflict between geom_text and ggplot(fill)

标签 r ggplot2

当我在 ggplot 上使用 geom_text 时,与 ggplot 的“填充”选项发生冲突。

这是问题的一个明显例子:

library(ggplot2)
a=ChickWeight
str(a)
xx=data.frame(level=levels(a$Chick),letter=1:50)

# a graph with the fill option alone
x11();ggplot(a, aes(x=Chick, y=weight,fill=Diet))  + geom_boxplot(notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
xlab("Chick") +
ylab("Weight")

# a graph with the geom_text option alone
x11();ggplot(a, aes(x=Chick, y=weight))  + geom_boxplot(notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
geom_text(data=xx, aes(x=level,y=450,label = letter)) +
xlab("Chick") +
ylab("Weight")

# a graph with the two option
x11();ggplot(a, aes(x=Chick, y=weight,fill=Diet))  + geom_boxplot(notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
geom_text(data=xx, aes(x=level,y=1750,label = letter)) +
xlab("Chick") +
ylab("Weight")

最佳答案

如果您只希望填充影响箱线图,请将 aes() 移动到箱线图中。 ggplot() 调用本身中的任何 aes() 美学都将传播到所有层

ggplot(a, aes(x=Chick, y=weight))  + geom_boxplot(aes(fill=Diet), notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
geom_text(data=xx, aes(x=level,y=1750,label = letter)) +
xlab("Chick") +
ylab("Weight")

您还可以使用 fill=NULL 禁用文本层中的 fill= 美学

ggplot(a, aes(x=Chick, y=weight, fill=Diet))  + geom_boxplot(notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
geom_text(data=xx, aes(x=level,y=1750,label = letter, fill=NULL)) +
xlab("Chick") +
ylab("Weight")

关于r - ggplot : conflict between geom_text and ggplot(fill),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30146970/

相关文章:

r - R中带有分组列的ggplot2条形图,如何取消堆叠?

mysql - ssh端口从R转发到mysql

r - 如何在指定值的线图中添加阴影置信区间

r - 如何按组计算 Z-score

r - 如何在ggplot中给QQ图上的点着色

r - 指定限制时从 ggplot2 直方图中删除的值

html - 在 Shiny tabpanel 上渲染 HTML

r - 合并不同大小的数据框和重复值

r - 如何使用R ggplot stat_summary绘制中位数和四分位数?

r - ggplot 中的问题,连续 x 轴为年份