我很难为具有黄土回归的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,color = "blue",size = 3) +
geom_smooth(method = "loess",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"),fill = "blue") +
scale_color_manual(name = "légende",labels = c("Data","Moy","loess regression \n with confidence interval"),values = c("darkgray","royalblue","red"))
似乎所有图例设置都以一种“怪异”的方式混合在一起,一个是用灰线覆盖的灰色点,然后是蓝色和红色(对于3个标签)相同。所有人都有蓝色的背景: