【问题标题】:Add legend using geom_point and geom_smooth from different dataset使用来自不同数据集的 geom_point 和 geom_smooth 添加图例
【发布时间】:2019-11-08 17:17:10
【问题描述】:

我真的很难为带有黄土回归的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 个标签)。所有的背景都被填充为蓝色:

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    如果您需要标记均值,可能需要有点创意,因为在 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")
    

    【讨论】:

    • 非常感谢您的回答,这正是我需要的完美答案!
    猜你喜欢
    • 2018-10-05
    • 2022-11-27
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多