Nawa Raj
Nawa Raj

Reputation: 111

Override the aesthetics in the legend using ggplot

Purpose of this question is: how to combine the dot and line separately in the legend box using ggplot. Here is my data set: It consist of time, actual data points and predictive data points of two different models

time <- seq(2009.25, 2016, by=0.25)

act.data <- c(0, 0, 1, 15, 29, 42, 59, 79, 93, 135, 159, 181, 194, 208, 216, 225, 
              272, 285, 308, 325, 331, 338, 348, 361, 396, 429, 481, 508)

pred.data_1 <- c(26.94136, 31.92765, 37.76609, 44.57464, 52.47702, 61.59878, 72.06163, 
                 83.97596, 97.43151, 112.48662, 129.15672, 147.40328, 167.12485, 188.15196, 
                 210.24759, 233.11426, 256.40805, 279.75825, 302.79059, 325.15084, 346.52585, 
                 366.65943, 385.36152, 402.51061, 418.05014, 431.98047, 444.34828, 455.23510)

pred.data_2 <- c(25.54623, 33.01310, 41.26586, 50.30449, 60.12899, 70.73938, 82.13564, 
                 94.31777, 107.28579, 121.03968, 135.57944, 150.90509, 167.01661, 183.91400, 
                 201.59728, 220.06643, 239.32146, 259.36236, 280.18914, 301.80180, 324.20034, 
                 347.38475, 371.35504, 396.11120, 421.65324, 447.98116, 475.09496, 502.99463)

data.frame(
  time=time, 
  pred_1=pred.data_1, 
  pred_2=pred.data_2, 
  act.daa=act.data
) -> xdf

I want to produce my graph using predicted values of the two models in terms of different color line and actual data points using dots.Here is the code:

cols <- c("AM"="#000000", "RL"="#0000FF")
br_x <- seq(2009, 2016, by=1)
br_y <- seq(1, 525, by=125)

ggplot(xdf, aes(time, act.data)) +
  geom_point() +
  geom_line(aes(y=pred_1, colour="AML"), size=0.2) +
  geom_line(aes(y=pred_2, colour="RL"), size=0.2) +
  scale_x_continuous(name="Year", breaks=br_x) +
  scale_y_continuous(name="Cumulative Data", breaks=br_y) +
  scale_colour_manual(name="MODEL", values=cols) +
  theme_bw()+
  theme(axis.title = element_text(face="bold"),
        legend.position = c(0.3,0.95),
        legend.justification = c("right","top"),
        legend.box.just = "left")

enter image description here

Once I run this code, two lines with different colors appear in legend but not the black dot. I want to make legends consisting of 2 lines with different colors and dot that represents the actual data points independently.

Upvotes: 1

Views: 359

Answers (1)

jazzurro
jazzurro

Reputation: 23574

You could do something like this. Since you want to show the actual data points, you need to have something in aes() in geom_point. So I created a dummy variable in data. I also rearranged your data for the two lines and created a data frame. I manually changed colors for the actual data points and the two lines using scale_fill_manual() and scale_color_manual(). You can add other necessary codes to the following code to make your own graphic. I hope this will help you.

library(tidyverse)

data <- data.frame(time, act.data, dummy = "")

lines <- data.frame(time = time, pred.data_1 = pred.data_1, pred.data_2 = pred.data_2) %>%
         gather(key = colnames, value = value, -time)

ggplot()+
geom_point(data = data, aes(x = time, y = act.data, fill = dummy))+
scale_fill_manual(name = "Actual data points", values = "black") +
geom_line(data = lines, aes(x = time, y = value, colour = colnames, group = colnames), size = 0.2) +
scale_color_manual(name = "Models", values = c("#000000", "#0000FF"), labels = c("AM", "RL"))

enter image description here

Upvotes: 2

Related Questions