将来自不同数据集的多条回归线的图例添加到ggplot [英] Add legend for multiple regression lines from different datasets to ggplot

查看:63
本文介绍了将来自不同数据集的多条回归线的图例添加到ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将4个数据库加载到R中,每个数据库都有2列:"Rating"和"Temperature".我想创建一个具有4条回归线(图中没有数据点)的额定值"和温度"的图形.我的代码是:

I have loaded 4 databases into R, each of them have 2 columns: "Rating" and "Temperature". I want to create a graph with 4 regression lines (without data points inside the plot) of Ratings and Temperature. 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.

我希望为4行添加一个图例,以说明哪种颜色用于哪种评级.将图例放在外面会更好.

I hope to add a legend for 4 lines to explain which color is for which Rating. Would be better to place the legend outside.

谢谢!

推荐答案

为放置在 aes()中的任何美感创建了传奇.通常,它们是通过从一个数据框中进行绘制并仅向该数据框中的一列提供 aes(color = )来创建的.(这是您的情况),您可以利用以下知识:每当在 aes()中放入一些与众不同的美学效果(例如 color = )时, ggplot 就会成为传奇.

Legends are created for any aesthetic that is placed in 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 = )中为将用作图例中的标签(键值标签)的每个点添加字符串.然后,您可以使用 scale_color_manual 定义图例的名称以及颜色本身.

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.

下面是一个示例,其中我在现有图上添加3个点,我想调出每个点的颜色.

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.

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')
  )

如果您在为特定颜色分配一个图例键时遇到麻烦,还可以将列表输入到 scale_color_manual values = 中,该列表定义了'key.name'='color.name'.

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.

这篇关于将来自不同数据集的多条回归线的图例添加到ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆