Search code examples
rggplot2corresponding-records

The geom_line of the corresponding two periods of data


There is data recording the changes in the two periods. The data correspond by id and are divided into groups. In other words, the data shows the change from 2000 to 2007 id1 in group A, .

type <- c("a","a","b","b","b","b","c","c","c","c","c","c")
year <- c(2000,2007,2000,2000,2007,2007,2000,2000,2000,2007,2007,2007)
rate <- c(0.1,0.4,0.2,0.1,0.4,0.3,0.3,0.3,0.4,0.6,0.8,0.4)
id <- c(1,1,2,3,2,3,4,5,6,4,5,6)
d<-cbind(type,year,rate,id)

I want to draw a line with two periods for each id, but it doesn't work well.

ggplot(data=d, mapping=aes(x=year, y=rate), color = type, group = id) +
  geom_line(aes(year, rate), linetype = "solid", size = 2, alpha = .4)

enter image description here


Solution

  • I don't know much about the context of your data, but from what you describe, you need to simply put the id and type into the aestetics.

    d<-data.frame(type,year,rate,id)
    
    ggplot(data=d, mapping=aes(x=year, y=rate, color = type, group = id)) +
      geom_line(linetype = "solid", size = 2, alpha = .4)
    

    enter image description here