R 如何显示趋势线的图例? 问题

R 如何显示趋势线的图例? 问题,r,ggplot2,visualization,legend,R,Ggplot2,Visualization,Legend,似乎我很难显示使用stat\u smooth()生成的趋势线。在使用argumentshow.legend=T之前,我有一个如下图: 添加参数后,我得到了如下结果: 但你看,我想单独显示趋势线图例,如下所示: 我如何做到这一点?如果您需要,我的源代码就在这里(如果您能帮助我截断代码使其更简洁,我将不胜感激): 谢天谢地,它展示了趋势线传奇。但仍然不理想: 如何改进代码?问题出在指南语句中。以下是代码中的数据可视化部分,已进行了一些修改: # Data Visualisation g &l

似乎我很难显示使用
stat\u smooth()
生成的趋势线。在使用argument
show.legend=T
之前,我有一个如下图:

添加参数后,我得到了如下结果:

但你看,我想单独显示趋势线图例,如下所示:

我如何做到这一点?如果您需要,我的源代码就在这里(如果您能帮助我截断代码使其更简洁,我将不胜感激):

谢天谢地,它展示了趋势线传奇。但仍然不理想:


如何改进代码?

问题出在
指南
语句中。以下是代码中的数据可视化部分,已进行了一些修改:

# Data Visualisation
g <- ggplot(data = econ, aes(CPI, HDI)) +
  geom_smooth(se = FALSE, method = 'lm', aes(group = 1, colour = "Trendline"), fullrange=T, linetype=1, formula=y~log(x)) +
  geom_point(stroke = 0, color = 'white', size = 3, show.legend = T) +
  scale_colour_manual(values = c("purple", "green", "blue", "yellow", "magenta", "orange", "red"))


g <- g + geom_point(aes(color = Region), size = 3, pch = 1, stroke = 1.2)

g <- g + theme_economist_white()

g <- g + scale_x_continuous(limits = c(1,10), breaks = 1:10) +
  scale_y_continuous(limits = c(0.2, 1.0), breaks = seq(0.2, 1.0, 0.1)) +
  labs(title = 'Corruption and human development',
       caption='Source: Transparency International; UN Human Development Report')


g <- g + xlab('Corruption Perceptions Index, 2011 (10=least corrupt)') +
  ylab('Human Development Index, 2011 (1=best)')

g <- g + theme(plot.title = element_text(family = 'Arial Narrow', size = 14, margin = margin(5, 0, 12, 0)),
               plot.caption = element_text(family = 'Arial Narrow', hjust = 0, margin=margin(10,0,0,0)),
               axis.title.x = element_text(family = 'Arial Narrow', face = 'italic', size = 8, margin = margin(10, 0, 10, 0)),
               axis.title.y = element_text(family = 'Arial Narrow', face = 'italic', size = 8, margin = margin(0, 10, 0, 10)),
               plot.background = element_rect(fill = 'white'),
               legend.title = element_blank()
) + theme(legend.background = element_blank(),
          legend.key = element_blank(),
          legend.text = element_text(family = 'Arial Narrow', size = 10))

g <- g + geom_text_repel(data = econ, aes(CPI, HDI, label = CountryLabel), family = 'Arial Narrow',
                         colour = 'grey10', force = 8, point.padding = 0.5, box.padding = 0.3,
                         segment.colour = 'grey10'
)

g + guides(colour = guide_legend(nrow = 1,
      override.aes = list(linetype = c(rep("blank", 6), "solid"),
                          shape = c(rep(1, 6), NA)
                          )
      )
    )
数据可视化
g可能会让你开始你的传奇问题。谢谢。我以前也参考过这篇文章。但是,我想让分散点的颜色保持原样。似乎我必须使用帖子提供的方法手动定义所有图例的颜色。@aosmith,我重做了该方法,并根据我的情况修改了它
scale\u color\u手册(值=c(“紫色”、“绿色”、“蓝色”、“黄色”、“洋红”、“橙色”、“红色”),guide=guide\u图例(override.aes=list(线型=c(代表(“空白”,7),“虚线”))
。它没有在图例区域显示我的趋势线,只是更改了其他点的颜色。您可以更新您的问题以显示您尝试的所有代码(我猜您在
geom_smooth
aes
中添加了
color
,就像在链接的答案中那样,因此线条应该显示在图例中)。在您的注释
override.aes
code中,看起来您定义了8种线型,但只有7个图例元素(6个区域加上线条).这可能会造成问题。@aosmith嗨。我用你的建议更新了我的问题和代码。虽然还有一些问题,但我想我正在接近。
g <- ggplot(data=econ, aes(CPI,HDI))+
  geom_smooth(se = FALSE, method = 'lm', aes(group = 1, colour = "Trendline"),fullrange=T, linetype=1,formula=y~log(x))+
  scale_colour_manual(values = c("purple", "green", "blue", "yellow",  "magenta","orange", "red"),
                      guides (colour = guide_legend (override.aes = list(linetype = 1)))

                      )+
  geom_point(...)
...
# Data Visualisation
g <- ggplot(data = econ, aes(CPI, HDI)) +
  geom_smooth(se = FALSE, method = 'lm', aes(group = 1, colour = "Trendline"), fullrange=T, linetype=1, formula=y~log(x)) +
  geom_point(stroke = 0, color = 'white', size = 3, show.legend = T) +
  scale_colour_manual(values = c("purple", "green", "blue", "yellow", "magenta", "orange", "red"))


g <- g + geom_point(aes(color = Region), size = 3, pch = 1, stroke = 1.2)

g <- g + theme_economist_white()

g <- g + scale_x_continuous(limits = c(1,10), breaks = 1:10) +
  scale_y_continuous(limits = c(0.2, 1.0), breaks = seq(0.2, 1.0, 0.1)) +
  labs(title = 'Corruption and human development',
       caption='Source: Transparency International; UN Human Development Report')


g <- g + xlab('Corruption Perceptions Index, 2011 (10=least corrupt)') +
  ylab('Human Development Index, 2011 (1=best)')

g <- g + theme(plot.title = element_text(family = 'Arial Narrow', size = 14, margin = margin(5, 0, 12, 0)),
               plot.caption = element_text(family = 'Arial Narrow', hjust = 0, margin=margin(10,0,0,0)),
               axis.title.x = element_text(family = 'Arial Narrow', face = 'italic', size = 8, margin = margin(10, 0, 10, 0)),
               axis.title.y = element_text(family = 'Arial Narrow', face = 'italic', size = 8, margin = margin(0, 10, 0, 10)),
               plot.background = element_rect(fill = 'white'),
               legend.title = element_blank()
) + theme(legend.background = element_blank(),
          legend.key = element_blank(),
          legend.text = element_text(family = 'Arial Narrow', size = 10))

g <- g + geom_text_repel(data = econ, aes(CPI, HDI, label = CountryLabel), family = 'Arial Narrow',
                         colour = 'grey10', force = 8, point.padding = 0.5, box.padding = 0.3,
                         segment.colour = 'grey10'
)

g + guides(colour = guide_legend(nrow = 1,
      override.aes = list(linetype = c(rep("blank", 6), "solid"),
                          shape = c(rep(1, 6), NA)
                          )
      )
    )