ggplot循环绘制具有匹配前缀但后缀不同的列

问题描述 投票:0回答:1

我有一个包含高,低和高 - 低的大数据框,用于每列。我还有一个Base列。我想为每组前缀创建一个图形,以便线图具有A_H,A_L,A_HL和Base,然后对所有其他匹配前缀都相同。

date     A_H B_H C_H D_H A_L B_L C_L D_L A_HL B_HL C_HL D_HL Base
2/1/18    6   4   6   4   2   3   5   8   9    2    3    5    3
2/2/18    2   4   7   6   5   8   3   9   11   12   5    9    5
2/3/18    8   6   8   9   6   9   7   9   13   13   6    7    5

我没有尝试过多种方法。

GraphList <- c("A", "B", "C", "D")
for (i in seq_along(GraphList)){
    plot <- ggplot(df, aes(date)) +
        geom_line(aes(y=Base, colour='Base')) +
        geom_line(aes(y=paste0(i,"High"), colour='High')) +
        geom_line(aes(y=paste0(i,"Low"), colour='Low')) +
        geom_line(aes(y=paste0(i,"LS"), colour='LS')) 
    print(plot)

但是,当我执行上述操作时,图表不会粘贴列表中的名称前缀,它只会将1H和1L,2H和2L等作为平行线吐出各自的图形。

我也试过了

plot <- ggplot(df, aes(date)) +
        geom_line(aes(y=Base, colour='Base')) +
        geom_line(aes(y=df[, grepl("_H", colnames(df))], colour='High')) +
        geom_line(aes(y=df[, grepl("_L", colnames(df))], colour='Low')) +
        geom_line(aes(y=df[, grepl("_LS", colnames(df))], colour='LS')) 
    print(plot)

使用这种方法我得到了错误

Don't know how to automatically pick the scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous

Error: aesthetics must be either length 1 or the same as the data (63): y, colour, x

先感谢您。

r for-loop ggplot2 paste grepl
1个回答
1
投票

首先,如果将数据重新整形为“长”格式,我们可以让ggplot为我们做很多工作:

df <- read.table(text = 'date     A_H B_H C_H D_H A_L B_L C_L D_L A_HL B_HL C_HL D_HL Base
2/1/18    6   4   6   4   2   3   5   8   9    2    3    5    3
                 2/2/18    2   4   7   6   5   8   3   9   11   12   5    9    5
                 2/3/18    8   6   8   9   6   9   7   9   13   13   6    7    5', header = T, stringsAsFactors = F)

library(tidyverse)
library(lubridate)

df.long <- df %>% 
  tidyr::gather(variable, value, -date, -Base) %>% 
  separate(variable, into = c('variable', 'measure'), sep = '_') %>% 
  mutate(date = mdy(date))

         date Base variable measure value
1  2018-02-01    3        A       H     6
2  2018-02-02    5        A       H     2
3  2018-02-03    5        A       H     8
4  2018-02-01    3        B       H     4
5  2018-02-02    5        B       H     4
6  2018-02-03    5        B       H     6
7  2018-02-01    3        C       H     6
8  2018-02-02    5        C       H     7
9  2018-02-03    5        C       H     8
10 2018-02-01    3        D       H     4

df.long将“Base”移动到它自己的列中,其值重复每个级别的“变量”(A,B,C,D)和“measure”(H,L,HL)。我还将“日期”列转换为正确的日期数据,这也将允许ggplot为我们做更多的工作。

首先,我们可以在一个方面的情节中拥有所有这些:

g <- ggplot(data = df.long, aes(x = date, y = value, color = measure)) +
  geom_line() +
  geom_line(aes(y = Base), color = 'black') +
  facet_grid(facets = ~variable)
print(g)

enter image description here

或者我们可以使用循环来创建几个单独的绘图对象:

plots <- list()
for (i in unique(df.long$variable)) {
  plots[[i]] <- ggplot(data = filter(df.long, variable == i), aes(x = date, y = value, color = measure)) +
    geom_line() +
    geom_line(aes(y = Base), color = 'black')
}

plots[[1]]

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.