Home > Blockchain >  Can't do a multiple line plot with ggplot2
Can't do a multiple line plot with ggplot2

Time:11-14

I have a dataset with a column of unemployment, a column of months, and one for years.

I want to do a line plot where I have month number on the x axis, unemployment on the y axis and that each line represents a different year.

I first filtered the dataframe by year to have the y values for each year individually and I tried the following code:

y1 = df %>% filter(year == 1996) 
y1 = y1$unemploy
y2 = df %>% filter(year == 1997)
y2 = y2$unemploy
y3 = df %>% filter(year == 1998)
y3 = y3$unemploy 

plot1 = ggplot()    
  geom_line(mapping = aes(x = df$month, y = y1), color = "navyblue")  
  geom_line(mapping = aes(x = df$month,y = y2), color = "black")  
  geom_line(mapping = aes(x = df$month,y = y3), color = "red")  
  scale_y_continuous(limits=c(0,10))  
  scale_x_continuous(limits=c(1,15)) 
plot1

But when I try to print the plot, I get the following error message:

Error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (128): y
Run `rlang::last_error()` to see where the error occurred.

Does anyone know what could be the problem with this plot?

CodePudding user response:

For the method you are using, you need to use a different dataset for each geom; you assign all the rows for variable month in your df to x and only a subset of the unemploy column to y, hence, different number of x and y entities in ggplot returns an error.

y1 = df %>% filter(year == 1996) 

y2 = df %>% filter(year == 1997)

y3 = df %>% filter(year == 1998)


plot1 = ggplot()    
  geom_line(y1, aes(x = month, y = unemploy), color = "navyblue")  
  geom_line(y2, aes(x = month, y = unemploy), color = "black")  
  geom_line(y3, aes(x = month, y = unemploy), color = "red")  
  scale_y_continuous(limits=c(0,10))  
  scale_x_continuous(limits=c(1,15)) 
plot1

But better practice is using color within mapping:

df %>%
  filter(year %in% c("1996", "1997", "1998")) %>%
ggplot()    
  geom_line(aes(x = month, y = unemploy, color = year))  
  scale_y_continuous(limits=c(0,10))  
  scale_x_continuous(limits=c(1,15)) 

You can use scale_color_manual later, if you want those specific colors and don't like the default ggplot colors.

CodePudding user response:

Rather than filtering by year and including three separate geom_lines(), just pass the full dataframe to ggplot() and map year to the color and group aesthetics. You can specify your colors using scale_color_manual().

library(ggplot2)
library(scales)

ggplot(data = df)  
  geom_line(aes(month, unemploy, color = year, group = year))  
  scale_color_manual(values = c("navyblue", "black", "red"))  
  scale_y_continuous(
    limits = c(0, .1),
    label = percent
  )  
  theme_light()

Example data:

set.seed(13)
df <- expand.grid(month = factor(1:12), year = factor(1996:1998))
unemploy <- vector("double", 36)
unemploy[[1]] <- .05
for (i in 2:36) {
  unemploy[[i]] <- unemploy[[i - 1]]   rnorm(1, 0, .005)
}
df$unemploy <- unemploy
  • Related