【问题标题】:ggplot loop graphing columns with matching prefix but different suffixggplot循环图形列具有匹配的前缀但不同的后缀
【发布时间】:2019-03-17 15:12:14
【问题描述】:

我有一个大型数据框,其中每一列都包含一个高、低和高低。我也有一个 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

提前谢谢你。

【问题讨论】:

  • 请通过包含充分代表您的实际数据的示例数据集(因为您说它很大)来解决您的问题reproducible。顺便说一句,我猜您应该通过将数据从宽格式转换为长格式来预处理数据,然后再将其传递给ggplot()
  • 这个样本代表我的实际数据。实际数据仅包含更多日期和更多字母(即 A-CA)。从宽格式到长格式的预处理会做什么?

标签: r for-loop ggplot2 paste grepl


【解决方案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)和“度量”(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)

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

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]]

【讨论】:

  • 非常感谢!这很有帮助。有没有办法将这些图表导出为 pdf?
  • 查看ggsave函数。
  • 好的,谢谢。最后一个问题。如果在我的真实数据集中,列名中有多个“”,有没有办法在“H”、“L”或“LS”之前的“”处指定分隔符"而不是在那之前?
  • 您可能想在 StackOverflow 上搜索该问题的答案。
猜你喜欢
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 2021-04-22
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多