简体   繁体   English

循环图 ggplot y for x 用于线性回归的不同类别,如何依次 plot 类别?

[英]Loop graphs ggplot y for x for different categories with linear regression, How to consequetively plot categories?

EDITED: I have a large data base trying to reapeatedly assess energy expenditue over time with the aim to compare multiple different variables (0/1, eg presence of severe head trauma vs. no such).已编辑:我有一个大型数据库,试图随着时间的推移重复评估能量消耗,目的是比较多个不同的变量(0/1,例如存在严重的头部外伤与否)。 The graph analysis should be repeated for all available variables in the database.应该对数据库中的所有可用变量重复图形分析。 All tables should be exported to a PDF File.所有表都应导出到 PDF 文件。

Currently I'm using the following code:目前我正在使用以下代码:

library(tidyverse)
library(ggpmisc)
my_data %>%
pdf(file="Plots.pdf" )
print(colnames(my_data) %>%
        map(function(x) my_data%>%
              ggplot(aes(x = Day, 
                         y = REE,
                         color=as_factor(x)))+
              scale_x_continuous(breaks = c(0,2,4,6,8,10,12,14,16,18,20,22,24,26,28))+
              scale_y_continuous(limits= c(0000,4000))+
              geom_point()+
              geom_smooth(method=lm,
                          se=TRUE,
                          size=2/10,
                          aes(group=as_factor(x)))+
              stat_poly_eq(aes(label = paste(after_stat(eq.label),
                                             after_stat(rr.label), 
                                             after_stat(p.value.label),
                                             sep = "*\", \"*")),
                           label.y="bottom", label.x="right")+
              labs(x="Time [d]",
                   y="Resting Energy Expenditure [kcal]")+
              scale_colour_grey(start=0.7,
                                  end=0.3)+
              theme_bw()
))
dev.off()

It generates the PDF File with all graphs.它生成包含所有图形的 PDF 文件。 However, it does not group/color according to the as_factor(x) and all data points are categorised into the same group.但是,它不会根据 as_factor(x) 进行分组/着色,并且所有数据点都归入同一组。

Does anyone have a possible explanation on how to resolve this problem that the categorising according to the factor variable doesn't work?是否有人对如何解决根据因子变量进行分类不起作用的问题有可能的解释?

The issue is that you loop over column names which are character strings.问题是您遍历作为字符串的列名。 Doing color=as.factor(x) you are mapping a constant character string on the color aes, ie you are doing something like color="foo" .执行color=as.factor(x)您正在将一个常量字符串映射到color aes 上,即您正在执行类似color="foo"的操作。 To tell ggplot2 that you want to map the data column whose name stored in x on the color aes you have to use eg the .data pronoun, ie do color=as.factor(.data[[x]]) .要告诉ggplot2您想要 map 数据列,其名称存储在颜色 aes 上的x中,您必须使用例如.data代词,即执行color=as.factor(.data[[x]])

Using a minimal reprex based on mtcars :使用基于mtcars的最小表示:

Note: Personally I would suggest to put your plotting code in a separate function ainstead of passing it as an anonymous function to purrr::map as I do below.注意:我个人建议将您的绘图代码放在单独的 function 中,而不是将其作为匿名 function 传递给purrr::map ,如下所示。 Makes debugging easier and your code cleaner.使调试更容易,代码更干净。

library(tidyverse)
library(ggpmisc)

my_data <- mtcars

plot_fun <- function(x) {
  ggplot(my_data, aes(
    x = mpg,
    y = hp,
    color = as_factor(.data[[x]])
  )) +
    #scale_x_continuous(breaks = c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28)) +
    #scale_y_continuous(limits = c(0000, 4000)) +
    geom_point() +
    geom_smooth(
      method = lm,
      se = TRUE,
      size = 2 / 10,
      aes(group = as_factor(x))
    ) +
    stat_poly_eq(aes(label = paste(after_stat(eq.label),
      after_stat(rr.label),
      after_stat(p.value.label),
      sep = "*\", \"*"
    )),
    label.y = "bottom", label.x = "right"
    ) +
    labs(
      x = "Time [d]",
      y = "Resting Energy Expenditure [kcal]"
    ) +
    scale_colour_grey(
      start = 0.7,
      end = 0.3
    ) +
    theme_bw()
}

cols <- c("cyl", "am", "gear") # colnames(my_data)

#pdf(file = "Plots.pdf")
purrr::map(cols, plot_fun)
#> [[1]]
#> `geom_smooth()` using formula 'y ~ x'

#> 
#> [[2]]
#> `geom_smooth()` using formula 'y ~ x'

#> 
#> [[3]]
#> `geom_smooth()` using formula 'y ~ x'

#dev.off()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM