简体   繁体   English

将来自不同数据集的多条回归线的图例添加到 ggplot

[英]Add legend for multiple regression lines from different datasets to ggplot

I have loaded 4 databases into R, each of them have 2 columns: "Rating" and "Temperature".我已经将 4 个数据库加载到 R 中,每个数据库都有 2 列:“评级”和“温度”。 I want to create a graph with 4 regression lines (without data points inside the plot) of Ratings and Temperature.我想创建一个带有 4 条回归线(图中没有数据点)的评级和温度图。 My code is:我的代码是:

y1<-Database1$`Rating1'
y2<-Database2$'Rating2'
y3<-Database3$`Rating3`
y4<-Database4$`Rating4`

x1<-Database1$`Mean Temperature (°C)`
x2<-Database2$`Mean Temperature (°C)`
x3<-Database3$`Mean Temperature (°C)`
x4<-Database4$`Mean Temperature (°C)`

reg1 <- lm(y1 ~ x1, data=Database1)
reg2 <- lm(y2 ~ x2, data=Database2)
reg3 <- lm(y3 ~ x3, data=Database3)
reg4 <- lm(y4 ~ x4, data=Database4)

equation1=function(x){coef(reg1)[2]*x+coef(reg1)[1]}
equation2=function(x){coef(reg2)[2]*x+coef(reg2)[1]}
equation3=function(x){coef(reg3)[2]*x+coef(reg3)[1]}
equation4=function(x){coef(reg4)[2]*x+coef(reg4)[1]}

library(ggplot2)
ggplot(Database1,aes(`Mean Temperature (°C)`,`Rating1`))+
  ggtitle("Sub-ratings vs Temperature (°C)") +
  xlab("Temperature (°C)") + ylab("Sub-ratings")+
  stat_function(fun=equation1,geom="line",color="skyblue",size=1)+
  stat_function(fun=equation2,geom="line",color="orangered",size=1)+
  stat_function(fun=equation3,geom="line",color="turquoise",size=1)+
  stat_function(fun=equation4,geom="line",color="royalblue",size=1)+
  theme(
    plot.title = element_text(size=12,hjust = 0.5),
    axis.title.x = element_text(size=11),
    axis.title.y = element_text(size=11)
  )+ylim(2,5)

I have created a graph like this.我已经创建了这样的图表。见图片

I hope to add a legend for 4 lines to explain which color is for which Rating.我希望为 4 行添加一个图例来解释哪个颜色是哪个评级。 Would be better to place the legend outside.最好把图例放在外面。

Thank you!谢谢!

Legends are created for any aesthetic that is placed in aes() .为放置在aes()中的任何美学创建图例。 Typically, they are created by plotting from one data frame and just supplying aes(color= with a column from that data frame. In the absence of a column connecting the geoms for which you want to create a legend (which is your case), you can utilize the knowledge that ggplot will make a legend whenever you put some differentiating aesthetic (like color= ) within aes() .通常,它们是通过从一个数据框中绘制并仅提供aes(color=以及该数据框中的一列来创建的。在没有连接要为其创建图例的几何图形的列的情况下(这是您的情况),您可以利用ggplot的知识,只要您在aes()中放置一些有区别的美学(如color= ),就会成为传奇。

To add your legend, add character strings within aes(color= for each of your points that will serve as the label (key value label) in the legend. Then, you can use scale_color_manual to define the name of the legend as well as the colors themselves.要添加图例,请在aes(color=中为每个点添加字符串,这些点将用作图例中的 label(键值标签)。然后,您可以使用scale_color_manual定义图例的名称以及colors 自己。

The following is an example, where I'm adding 3 points to an existing plot and I want to call out the color of each.以下是一个示例,其中我向现有 plot 添加 3 个点,并且我想调出每个点的颜色。

df <- data.frame(x=1:10, y=1:10)
ggplot(df, aes(x, y)) +
  geom_point() + theme_bw() +
  geom_point(data=data.frame(x=2,y=4), size=3, aes(color='point1')) +
  geom_point(data=data.frame(x=3,y=9), size=3, aes(color='point2')) +
  geom_point(data=data.frame(x=1,y=6), size=3, aes(color='point3')) +
  scale_color_manual(
    name='My Points',
    values=c('blue', 'orangered', 'green3')
  )

在此处输入图像描述

If you are having trouble assigning specifically one legend key to a specific color, you can also feed a list into values= for scale_color_manual that defines 'key.name'='color.name' for each item.如果您在为特定颜色指定一个图例键时遇到问题,您还可以将一个列表输入到values= for scale_color_manual中,该列表为每个项目定义'key.name'='color.name'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM