首页 > 解决方案 > 来自唯一数据框的多个单独图表

问题描述

我知道有些主题是关于类似的问题,但即使使用那些我也无法自己解决问题。因此,如果这个主题出现重复,我很抱歉,但我有点卡住了。

我必须绘制近 40 张图表,代表 24 小时内的体温变化(研究中每个人的图表)。为此,我尝试使用dplyrggplot2包编写一个循环。您可能会在下面找到我的数据示例。有许多缺失值,但我认为这并不代表当前问题的问题。

structure(list(heures = structure(1:13, .Label = c("01:00:00", 
"03:00:00", "05:00:00", "07:00:00", "08:00:00", "10:00:00", "12:00:00", 
"13:30:00", "15:00:00", "17:00:00", "19:00:00", "21:00:00", "23:00:00"
), class = "factor"), x1= c(36.55, 36.5, 36.44444444, 
36.6, 36.86666667, 37.26, 37, NA, NA, 37.3, 37.1, 37, 35.6), 
    x2 = c(NA, 34.5, 35.4, 36.1, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA), x3 = c(36.9, 36.4, NA, NA, 36.9, 
    NA, NA, NA, NA, 37.5, 37.5, 36.9, 37.1), x4 = c(36, 
    35.8, NA, NA, NA, 37.4, 36.7, 36.3, NA, 37.5, 37, NA, NA)), class = "data.frame", row.names = c(NA, 
-13L))

到目前为止,我已经编写了以下代码,其中“indiv”是包含上述数据的数据框。

names <- c(colnames(indiv))

graph <- list()

test <- function(df, names) {

  for (i in 1:length(df)) {

    name <- names[i]

    stock <- df %>%
      filter(heures, !!name)

    graph[[i]] <- ggplot(data=stock, aes(x=heures, y=stock[,2])) +
      geom_point() +
      labs(x="Hours (HH:MM:SS)",
           y="Temperature",
           title=colnames(stock[2]))

  }

  return(graph)

}

它返回一个似乎表明过滤器功能无法正常工作的错误:

Warning messages:
 1: In Ops.factor(~heures, ~"x1") :
  ‘&amp;’ not meaningful for factors

我无法弄清楚我在这方面做错了什么。我还尝试了当前循环中没有 dplyr 部分的代码,但它也没有给我想要的输出。

提前感谢您的建议。

标签: rgraphdplyr

解决方案


我提出了这个想法:整理一下数据集,使其更易于与 ggplot 一起使用,然后将其拆分并将拆分后的数据框存储在列表中。然后我lapply避免使用循环和自定义函数来创建绘图。

如果您有大量数据,这不是一种非常快速的方法,但我在小型数据集上经常使用此技巧。此代码为每个人(而不是方面)创建一个图。

library(tidyverse) # all functions of these packages are not necessary here

df = structure(list(heures = structure(1:13, .Label = c("01:00:00", 
                                                   "03:00:00", "05:00:00", "07:00:00", "08:00:00", "10:00:00", "12:00:00", 
                                                   "13:30:00", "15:00:00", "17:00:00", "19:00:00", "21:00:00", "23:00:00"
), class = "factor"), x1= c(36.55, 36.5, 36.44444444, 
                            36.6, 36.86666667, 37.26, 37, NA, NA, 37.3, 37.1, 37, 35.6), 
x2 = c(NA, 34.5, 35.4, 36.1, NA, NA, NA, NA, NA, 
       NA, NA, NA, NA), x3 = c(36.9, 36.4, NA, NA, 36.9, 
                               NA, NA, NA, NA, 37.5, 37.5, 36.9, 37.1), x4 = c(36, 
                                                                               35.8, NA, NA, NA, 37.4, 36.7, 36.3, NA, 37.5, 37, NA, NA)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                               -13L))


# tidy your data, good practice makes it easier to plot things with ggplot
df = df %>% pivot_longer(2:ncol(df), names_to = "individual", values_to = "temperature")

# I would do it this way:
df_list = split(df, df$individual)

plot_fun = function(df) {
  title = unique(df$individual)
  ggplot(df, aes(x=heures, y=temperature))+
    geom_point() +
    labs(title = title)
  #### add here things to save your plots, store them somewhere, etc
}
lapply(df_list, FUN = plot_fun)

推荐阅读