ggplot line graph with NA values

前端 未结 3 2120
眼角桃花
眼角桃花 2020-12-31 12:08

I\'m having with trouble with ggplot trying to plot 2 incomplete time series on the same graph where the y data does not have the same values on the x-axis (year) - NAs are

3条回答
  •  甜味超标
    2020-12-31 12:18

    My preferred solution would be to reshape this to long format. Then you only need 1 geom_line call. Especially if you have many series, that's tidier. Same result as LyzandeR's 2nd chart.

    library(ggplot2)
    library(reshape2)
    
    test2 <- melt(test, id.var='YEAR')
    test2 <- na.omit(test2)
    
    ggplot(test2, aes(x=YEAR, y=value, color=variable)) + 
      geom_line() +
      scale_color_manual(values=c('red', 'green')) +
    
      xlab("Year") + ylab("Percent") +
      scale_x_continuous(limits=c(1935, 1995), breaks = seq(1935, 1995, 5),
                         expand = c(0, 0)) + 
      scale_y_continuous(limits=c(0,50), breaks=seq(0, 50, 10), expand = c(0, 0))
    

    enter image description here

    You might consider adding a geom_point() call in addition to the line, so it's clear which points are real values and which are missing. Another advantage to the long format is that additional geoms take just 1 call each, as opposed to 1 per series each.

    enter image description here

提交回复
热议问题