我很难为具有黄土回归的geom_point
图设置正确的图例,同时使用了2个数据集
我得到了一个数据集,该数据集汇总了一天中的活动,然后在同一张图上作了绘图,记录了每小时和每天的所有活动,以及用黄土函数平滑的回归曲线,以及每天的每个小时。
更准确地说,这是第一个代码的示例,返回的图没有图例,这正是我所期望的:
# first graph, which is given what I expected but with no legend
p <- ggplot(dat1, aes(x = Hour, y = value)) +
geom_point(color = "darkgray", size = 1) +
geom_point(data = dat2, mapping = aes(x = Hour, y = mean),
color = 20, size = 3) +
geom_smooth(method = "loess", span = 0.2, color = "red", fill = "blue")
和图形(灰色表示每小时,每天的所有数据。红色曲线表示黄土回归。蓝色点表示每小时的平均值)
当我尝试设置图例时,我无法用两种点(灰色数据,均值蓝色)和黄土曲线(红色)的解释来绘制图例。请参阅以下我尝试过的示例。
# second graph, which is given what I expected + the legend for the loess that
# I wanted but with not the dot legend
p <- ggplot(dat1, aes(x = Hour, y = value)) +
geom_point(color = "darkgray", size = 1) +
geom_point(data = dat2, mapping = aes(x = Hour, y = mean),
color = "blue", size = 3) +
geom_smooth(method = "loess", span = 0.2, aes(color = "red"), fill = "blue") +
scale_color_identity(name = "legend model", guide = "legend",
labels = "loess regression \n with confidence interval")
和另一个审判:
# I tried to combine both date set into a single one as following but it did not
# work at all and I really do not understand how the legends works in ggplot2
# compared to the normal plots
A <- rbind(dat1, dat2)
p <- ggplot(A, aes(x = Heure, y = value, color = variable)) +
geom_point(data = subset(A, variable == "data"), size = 1) +
geom_point(data = subset(A, variable == "Moy"), size = 3) +
geom_smooth(method = "loess", span = 0.2, aes(color = "red"), fill = "blue") +
scale_color_manual(name = "légende",
labels = c("Data", "Moy", "loess regression \n with confidence interval"),
values = c("darkgray", "royalblue", "red"))
似乎所有图例设置都以一种“怪异”的方式混合在一起,一个是用灰线覆盖的灰色点,然后是蓝色和红色(对于3个标签)相同。所有人都有蓝色的背景:
答案 0 :(得分:0)
如果您需要标记均值,则可能需要有点创意,因为在ggplot中手动添加图例并不容易。
我模拟的东西看起来像下面的数据。
dat1 = data.frame(
Hour = rep(1:24,each=10),
value = c(rnorm(60,0,1),rnorm(60,2,1),rnorm(60,1,1),rnorm(60,-1,1))
)
# classify this as raw data
dat1$Data = "Raw"
# calculate mean like you did
dat2 <- dat1 %>% group_by(Hour) %>% summarise(value=mean(value))
# classify this as mean
dat2$Data = "Mean"
# combine the data frames
plotdat <- rbind(dat1,dat2)
# add a dummy variable, we'll use it later
plotdat$line = "Loess-Smooth"
我们首先制作基本点图:
ggplot(plotdat, aes(x = Hour, y = value,col=Data,size=Data)) +
geom_point() +
scale_color_manual(values=c("blue","darkgray"))+
scale_size_manual(values=c(3,1),guide=FALSE)
请注意尺寸,我们将指南设置为FALSE,因此它不会出现。现在我们添加黄土平滑,引入图例的一种方法是引入线型,并且由于只有一组,因此您将只有一个变量:
ggplot(plotdat, aes(x = Hour, y = value,col=Data,size=Data)) +
geom_point() +
scale_color_manual(values=c("blue","darkgray"))+
scale_size_manual(values=c(3,1),guide=FALSE)+
geom_smooth(data=subset(plotdat,Data="Raw"),
aes(linetype=line),size=1,alpha=0.3,
method = "loess", span = 0.2, color = "red", fill = "blue")