【问题标题】:Assign name of data frame to ggplot title in purrr loop在 purrr 循环中将数据框的名称分配给 ggplot 标题
【发布时间】:2020-12-21 20:40:05
【问题描述】:

我有两个数据集,我想从中生成直方图,显示数据如何按名称(A、B、C)重叠。我已经编写了一个自定义函数,因此我可以将 ggplot 与 map2 一起使用。

我希望根据每个数据集的名称为图表命名,例如“A”、“B”、“C”。有谁知道这样做的方法吗?

# load packages 
library(ggplot2)
library(dplyr)
library(purrr)

## load and format data 1
df1_raw <- data.frame(name = c("A", "B", "C", "A", "C", "B"), 
                 start = c(1, 3, 4, 5, 2, 1),  
                 end = c(6, 5, 7, 8, 6, 7)) 
df1 <- split(x = df1_raw, f = df1_raw$name) # split data by name
df1 <- lapply(df1, function(x) Map(seq.int, x$start, x$end)) # generate sequence intervals
df1 <- map(df1, unlist) # unlist sequences
df1 <- lapply(df1, data.frame) # convert to df

## load and format data 2
df2_raw <- data.frame(name = c("C", "B", "C", "A", "A", "B"), 
                      start = c(5, 4, 3, 4, 4, 5),  
                      end = c(7, 8, 7, 6, 9, 6)) 
df2 <- split(x = df2_raw, f = df2_raw$name) # split data by name
df2 <- lapply(df2, function(x) Map(seq.int, x$start, x$end)) # generate sequence intervals
df2 <- map(df2, unlist) # unlist sequences
df2 <- lapply(df2, data.frame) # convert to df

## write custom ggplot function and generate graphs
gplot <- function(data1, data2) {
  ggplot() + 
    geom_histogram(data = data1, aes(x = X..i..),  binwidth = 1, color = "grey", fill = "grey") +
    geom_histogram(data = data2, aes(x = X..i..),  binwidth = 1, fill = "pink", alpha = 0.7) +
    labs(
      title = ls(data1))
}

hist <- map2(df1, df2, gplot)

我还在函数的标题字段中尝试了以下内容:

deparse(substitute(data1))

【问题讨论】:

  • 仅供参考 - 您需要反引号(在 1 键旁边),而不是单引号,用于代码格式化
  • 谢谢,我使用了错误的字符,我很困惑为什么它不起作用。
  • deparse(substitute(data1)) 是执行此操作的正常方法,但是将其放在 purrr 循环中会使事情变得更加困难。您最好的选择可能是编辑您的函数以采用 title 参数并将 names(df1) 添加到您正在循环的参数列表中。

标签: r ggplot2 purrr


【解决方案1】:

另一个与 @GregorThomas 在 cmets 中提到的类似的选项,您可以将 name 变量添加到您的 data.frames 并从您的 gplot() 函数中提取该变量。我还展示了如何组合一些数据操作步骤:

# load packages 
library(ggplot2)
library(dplyr)
library(purrr)

## load and format data 1
df1_raw <- data.frame(name = c("A", "B", "C", "A", "C", "B"), 
                      start = c(1, 3, 4, 5, 2, 1),  
                      end = c(6, 5, 7, 8, 6, 7)) 
df1 <- df1_raw %>%
  split(.$name) %>% # split data by name
  imap(function(x, x_name) {
    data.frame(value = Map(seq.int, x$start, x$end) %>% unlist,
               name = x_name)
  })

## load and format data 2
df2_raw <- data.frame(name = c("C", "B", "C", "A", "A", "B"), 
                      start = c(5, 4, 3, 4, 4, 5),  
                      end = c(7, 8, 7, 6, 9, 6)) 
df2 <- df2_raw %>%
  split(.$name) %>% # split data by name
  imap(function(x, x_name) {
    data.frame(value = Map(seq.int, x$start, x$end) %>% unlist,
               name = x_name)
  })

## change the title component of your previous function
gplot <- function(data1, data2) {
  ggplot() + 
    geom_histogram(data = data1, aes(x = value),  binwidth = 1, color = "grey", fill = "grey") +
    geom_histogram(data = data2, aes(x = value),  binwidth = 1, fill = "pink", alpha = 0.7) +
    ggtitle(data1$name[1])
}

## plot it
map2(df1, df2, gplot)

【讨论】:

    猜你喜欢
    • 2020-10-01
    • 1970-01-01
    • 2011-11-22
    • 2017-12-14
    • 2021-10-07
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    相关资源
    最近更新 更多