Home > Back-end >  plot several lines out of the same two columns dataframe R
plot several lines out of the same two columns dataframe R

Time:12-22

I would like to plot several lines of different time lenght in the same line graph out of the same dataframe. Then I want to set the X axis from 1 to X, being X the maximum number of periods of the line with maximum lenght.

For instance, line1 for years 1980 to 1989, line2 for years 1995 to 2001, etc. See data below for the data I use.

dput(malineGDP) structure(list(Year = c(1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = "GDPpercent", class = "factor"), value = c(0.0277183930261643, 0.0288427846071515, 0.0526348961735552, 0.0371487738221672, 0.0427232519007513, 0.0704405101613155, 0.0771983341458076, 0.07685960434821, 0.111917681446816, 0.082616933786008, 0.0426218080075034, 0.0287408712637852, 0.0283927466984115, 0.0463085646932952, 0.0569077180354995, 0.0872515547548894, 0.0929491742150426, 0.130132692280228, 0.200424443096016, 0.222006216429481, 0.191768530376496, 0.0955005462614614, 0.0476287715422711, 0.0583828349263007, 0.0823773327852261, 0.102928117429317, 0.133464508881022, 0.135900867106755, 0.0822682054263, 0.0606165184312332, 0.0652403453525391, 0.0799398361449388, 0.0612558039666617, 0.0721234864232935, 0.122718837613643, 0.132779681405992, 0.0954672321123404, 0.0904404973441826, 0.0940829197015593, 0.0882826864405067, 0.0534565360036942, 0.11095822366049)), row.names = c(NA, -42L), class = "data.frame")

I don't know how to start tbh

CodePudding user response:

using ggplot2 for graphs

malineGDP$line=""
malineGDP$line[malineGDP$Year>=1980 & malineGDP$Year<=1989]="line1"
malineGDP$line[malineGDP$Year>=1995 & malineGDP$Year<=2001]="line2"

malineGDP$X=ave(malineGDP$Year,malineGDP$line,FUN=function(i){i-min(i) 1})

library(ggplot2)

ggplot(malineGDP,aes(y=value,x=X,group=line,color=line))   geom_line()  
  theme_minimal()

enter image description here

CodePudding user response:

library(tidyverse)

df %>% 
  mutate(period = case_when(between(Year, 1980, 1989) ~ "1980-1989", 
                          between(Year, 1995, 2001) ~ "1995-2001", 
                          TRUE ~ "Other")) %>% 
  group_by(period) %>%  
  mutate(time = 1:n()) %>% 
  ggplot()   
  aes(x = time, y = value, col = period)   
  geom_line(size = 1.5)   
  theme_light()

enter image description here

Without "Other"

df %>% 
  mutate(period = case_when(between(Year, 1980, 1989) ~ "1980-1989", 
                          between(Year, 1995, 2001) ~ "1995-2001", 
                          TRUE ~ "Other")) %>% 
  filter(period != "Other") %>% 
  group_by(period) %>%  
  mutate(time = 1:n()) %>% 
  ggplot()   
  aes(x = time, y = value, col = period)   
  geom_line(size = 1.5)   
  theme_light()

enter image description here

CodePudding user response:

You could put years in a vector, initialize an empty plot, and use lines on a subset of the data frame which lengths are adjusted by max length.

y <- list(1980:1989, 1995:2001, 2017:2021)

plot(seq_len(max(lengths(y))), ylim=range(dat$value), type='n', ylab='value', main='plot')
lapply(y, \(y) dat[dat$Year %in% y, 'value']) |>
  lapply(`length<-`, max(lengths(y))) |>
  Map(lines, x=_, col=seq_along(y)   1)
legend('topright', legend=lapply(y, range) |> lapply(paste, collapse=':'), lty=1, col=seq_along(y)   1)

enter image description here


Data:

dat <- structure(list(Year = c(1980, 1981, 1982, 1983, 1984, 1985, 1986, 
1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 
1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 
2020, 2021), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), levels = "GDPpercent", class = "factor"), value = c(0.0277183930261643, 
0.0288427846071515, 0.0526348961735552, 0.0371487738221672, 0.0427232519007513, 
0.0704405101613155, 0.0771983341458076, 0.07685960434821, 0.111917681446816, 
0.082616933786008, 0.0426218080075034, 0.0287408712637852, 0.0283927466984115, 
0.0463085646932952, 0.0569077180354995, 0.0872515547548894, 0.0929491742150426, 
0.130132692280228, 0.200424443096016, 0.222006216429481, 0.191768530376496, 
0.0955005462614614, 0.0476287715422711, 0.0583828349263007, 0.0823773327852261, 
0.102928117429317, 0.133464508881022, 0.135900867106755, 0.0822682054263, 
0.0606165184312332, 0.0652403453525391, 0.0799398361449388, 0.0612558039666617, 
0.0721234864232935, 0.122718837613643, 0.132779681405992, 0.0954672321123404, 
0.0904404973441826, 0.0940829197015593, 0.0882826864405067, 0.0534565360036942, 
0.11095822366049)), row.names = c(NA, -42L), class = "data.frame")
  •  Tags:  
  • r
  • Related