ggplot:geom_text和ggplot(fill)之间的冲突

3

当我在ggplot中使用geom_text时,会与ggplot的“fill”选项发生冲突。

以下是问题的清晰示例:

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")
1个回答

4

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接