簡體   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