|
|
1
pogibas
7 年前
给定的解决方案在大多数情况下都有效,但不适用于
geom-hline
(
vline
)。对于它们,您通常不必使用
aes
,但当您需要生成一个图例时,必须将它们包装在
aes
:。
库(ggplot2)
GGPROTH()+
地理线(aes(count,mean,color=“truemean”),mydf)+
geom_h hline(aes(yintercept=mytruemean,color=“samplemean”))+
比例颜色手册(值=C(“红色”、“蓝色”))+
labs(title=“显示平均值收敛的绘图”,
x=“索引”,
Y=“平均”,
颜色=空)+
最小极小()
< /代码>

查看原始数据,您可以使用geom_point以获得更好的可视化效果(还添加了一些主题更改):。
ggplot()。+
几何点(aes(count,mean,color=“observated”),mydf,
α=0.3,尺寸=0.7)+
geom_h hline(aes(yintercept=mytruemean,color=“expected”),
线型=2,尺寸=0.5)+
比例颜色手册(值=C(“蓝色”、“红色”))+
labs(title=“显示平均值收敛的绘图”,
x=“索引”,
Y=“平均”,
color=“mean type”)。+
主题_minimal()。+
参考线(颜色=参考线图例(override.aes=列表(
线型=0,大小=4,形状=15,alpha=1)
)
< /代码>

,但当需要生成一个图例时,必须将它们包装在俄歇电子能谱:
library(ggplot2)
ggplot() +
geom_line(aes(count, mean, color = "TrueMean"), myDf) +
geom_hline(aes(yintercept = myTrueMean, color = "SampleMean")) +
scale_colour_manual(values = c("red", "blue")) +
labs(title = "Plot showing convergens of Mean",
x = "Index",
y = "Mean",
color = NULL) +
theme_minimal()

查看可以使用的原始数据geom_point为了更好地可视化(也增加了一些主题变化):
ggplot() +
geom_point(aes(count, mean, color = "Observed"), myDf,
alpha = 0.3, size = 0.7) +
geom_hline(aes(yintercept = myTrueMean, color = "Expected"),
linetype = 2, size = 0.5) +
scale_colour_manual(values = c("blue", "red")) +
labs(title = "Plot showing convergens of Mean",
x = "Index",
y = "Mean",
color = "Mean type") +
theme_minimal() +
guides(color = guide_legend(override.aes = list(
linetype = 0, size = 4, shape = 15, alpha = 1))
)

|