使用 geom_point 在 R 中绘制多列

plotting multiple columns in R using geom_point

我有一个如下所示的数据框:

共 36 列。

数据示例代码:

df <-
  
  structure(
    list(
      Bacteroidaceae = c(
        0,
        0.10944999,
        0.104713314,
        0.125727668,
        0.124136247,
        0.005155911,
        0.005072778,
        0.010231826,
        0.010188139
        
      ),
      Christensenellaceae = c(
        0,
        0.009910731,
        0.010131195,
        0.009679938,
        0.01147601,
        0.010484508,
        0.008641566,
        0.010017172,
        0.010741488
        
      ),
      
      treatment = c(
        "Original Sample1",
        "Original Sample2",
        "Original Sample3",
        "Original Sample4",
        "treatment1_1",
        "treatment1_2",
        "treatment1_3",
        "treatment1_4"
      )
    ),
    class = "data.frame",
    row.names = c(NA,-8L)
  )

我想做的是为数据中的所有列创建图,这样每个列都会有 2 个图:一个用于处理 1,一个用于原始样本,总共 72 个图

例如:

与原始样本类型相同 我尝试使用此代码:

df %>%
  tidyr::pivot_longer(!treatment, names_to = "taxa", values_to = "value") %>%
  dplyr::filter(str_detect(treatment, "Treatment1")) %>%
   for (i in columns(df)){
    )
  ggplot(aes(x = treatment, y = value, color = taxa),group=treatment) +
  geom_point() +
  stat_summary(fun.y = mean,
               geom = "line", width = 0.5)+geom_jitter(widh=0.25)
  theme_bw()}}

但是没用。 还有其他方法吗?

谢谢

也许这就是您要找的:

library(tidyverse)

df %>% 
  pivot_longer(-treatment) %>% 
  mutate(plot = ifelse(str_detect(treatment, "Original"), 
                       "Original Sample", 
                       "Treatment 1"),
         treatment = str_extract(treatment, "\d+$")) %>% 
  group_by(name) %>% 
  group_split() %>% 
  map(~.x %>% ggplot(aes(x = factor(treatment), y = value, color = factor(name))) +
         geom_point() +
         facet_wrap(~plot) +
         labs(x = "Treatment", y = "Value", color = "Taxa") +
         theme_bw())

这会产生两个图(基于测试数据):