繁体   English   中英

从相同的两列数据框 R 中绘制几行

[英]plot several lines out of the same two columns dataframe R

我想在同一个数据帧的同一个折线图中绘制几条不同时间长度的线。 然后我想将 X 轴设置为从 1 到 X,X 是具有最大长度的直线的最大周期数。

例如,1980 年至 1989 年的 line1,1995 年至 2001 年的 line2,等等。请参阅下面的数据了解我使用的数据。

dput(malineGDP) 结构(列表(年 = c(1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1997 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 20219), = 结构20219, = (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.058382834 9263007, 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")

我不知道如何开始

使用 ggplot2 作图

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()

在此处输入图像描述

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()

在此处输入图像描述

没有“其他”

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()

在此处输入图像描述

您可以将y耳朵放在一个向量中,初始化一个空plot ,并在数据框的子集上使用lines ,这些线的lengthsmax长度调整。

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)

在此处输入图像描述


数据:

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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM