如何使用ggplot2向复杂的散点图添加新图例

时间:2017-06-14 06:17:52

标签: r ggplot2 legend

我构建了一个简单的线性回归模型,并使用该模型生成了一些预测值。但是,我更感兴趣的是在图表上将其可视化,但我不知道如何添加图例以将原始 mpg 值突出显示为“黑色”和新的预测值为“红色”。

此示例中使用的数据是来自数据集包的 mtcars 数据集

    library(ggplot2)

    library(datasets)
    library(broom)

    # Build a simple linear model between hp and mpg

    m1<-lm(hp~mpg,data=mtcars)

    # Predict new `mpg` given values below 

    new_mpg = data.frame(mpg=c(23,21,30,28))

    new_hp<- augment(m1,newdata=new_mpg)

    # plot new predicted values in the graph along with original mpg values

    ggplot(data=mtcars,aes(x=mpg,y=hp)) + geom_point(color="black") + geom_smooth(method="lm",col=4,se=F) + 
      geom_point(data=new_hp,aes(y=.fitted),color="red") 

enter image description here

散点图

2 个答案:

答案 0 :(得分:3)

这是一个想法。您可以将预测数据和观察数据组合在同一数据框中,然后创建散点图以生成图例。以下代码是现有代码的扩展。

# Prepare the dataset
library(dplyr)

new_hp2 <- new_hp %>%
  select(mpg, hp = .fitted) %>%
  # Add a label to show it is predicted data
  mutate(Type = "Predicted")

dt <- mtcars %>%
  select(mpg, hp) %>%
  # Add a label to show it is observed data
  mutate(Type = "Observed") %>%
  # Combine predicted data and observed data
  bind_rows(new_hp2)

# plot the data
ggplot(data = dt, aes(x = mpg, y = hp, color = factor(Type))) + 
  geom_smooth(method="lm", col = 4, se = F) +
  geom_point() +
  scale_color_manual(name = "Type", values = c("Black", "Red"))

答案 1 :(得分:2)

以下是另一种不使用dplyr的方式:

ggplot() + 
  geom_point(data = mtcars, aes(x = mpg, y = hp, colour = "Obs")) +
  geom_point(data = new_hp, aes(x = mpg, y = .fitted, colour = "Pred")) +
  scale_colour_manual(name="Type",  
                      values = c("black", "red")) +
  geom_smooth(data = mtcars, aes(x = mpg, y = hp),
              method = "lm", col = 4, se = F)