【问题标题】:Combine data from 2 different lists of dataframes and 1 dataframe to create a list of charts using GGplot R结合来自 2 个不同的数据框列表和 1 个数据框的数据,使用 GGplot R 创建图表列表
【发布时间】:2021-12-03 10:09:06
【问题描述】:

我想结合来自多个来源的信息来绘制一系列图表的不同方面。不同的图表代表不同的元素,但对于每个图表,我试图将箱线图、散点图、与方程的最佳拟合线、每种分析物的平均值线和每个分析物的 3 个标准差相结合。我有我想要绘制的所有数据,并且坚持组装它们以获得所需的输出。

我正在寻找的是类似于下图的内容,但对于我的数据框中的每个分析物

我的列表的一个 sn-p

df <- structure(list(SampleNo = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L, 11L, 
11L, 12L, 12L), .Label = c("151868", "151959", "152253", "151637", 
"152382", "152490", "152528", "152581", "152985", "152738", "153005", 
"153337"), class = "factor"), Rep_No = c("1", "2", "1", "2", 
"1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", 
"2", "1", "2", "1", "2", "1", "2"), Fe = c(57.24, 57.12, 57.2, 
57.13, 57.21, 57.14, 57.16, 57.31, 57.11, 57.18, 57.21, 57.12, 
57.14, 57.17, 57.1, 57.18, 57, 57.06, 57.13, 57.09, 57.17, 57.23, 
57.09, 57.1), SiO2 = c(6.85, 6.83, 6.7, 6.69, 6.83, 6.8, 6.76, 
6.79, 6.82, 6.82, 6.8, 6.86, 6.9, 6.82, 6.81, 6.83, 6.79, 6.76, 
6.8, 6.88, 6.83, 6.79, 6.8, 6.83), Al2O3 = c(2.9, 2.88, 2.88, 
2.88, 2.92, 2.9, 2.89, 2.87, 2.9, 2.89, 2.9, 2.89, 2.89, 2.88, 
2.89, 2.91, 2.91, 2.91, 2.9, 2.9, 2.91, 2.91, 2.88, 2.86)), class = "data.frame", row.names = c(NA, 
-24L))

我的第一个列表数据

l1 <- list(Fe = structure(list(hm = 57.2, hsd = 0.295156858558032, 
    hmin = 56.3145294243259, hmax = 58.0854705756741), class = "data.frame", row.names = c(NA, 
-1L)), SiO2 = structure(list(hm = 6.7497718955, hsd = 0.111404744433739, 
    hmin = 6.41555766219878, hmax = 7.08398612880122), class = "data.frame", row.names = c(NA, 
-1L)), Al2O3 = structure(list(hm = 2.8925, hsd = 0.0725002768867193, 
    hmin = 2.67499916933984, hmax = 3.11000083066016), class = "data.frame", row.names = c(NA, 
-1L)))


l2 <- list(Fe = "italic(y) == \"1.5\" + \"0.000000000000001\" %.% italic(x) * \",\" ~ ~italic(r)^2 ~ \"=\" ~ \"0.00000000000000000000000000000114\"", 
    SiO2 = "italic(y) == \"1.5\" + \"0.000000000000001\" %.% italic(x) * \",\" ~ ~italic(r)^2 ~ \"=\" ~ \"0.00000000000000000000000000000114\"", 
    Al2O3 = "italic(y) == \"1.5\" + \"0.000000000000001\" %.% italic(x) * \",\" ~ ~italic(r)^2 ~ \"=\" ~ \"0.00000000000000000000000000000114\"")

我的代码似乎最能帮助我


library(ggplot)
library(tidyverse)
library(scales) 
library(ggpmisc)

H.PlotOrder <- unique(df$SampleNo) 
H.Charts <- df %>% mutate(SampleNo = factor(SampleNo, levels = H.PlotOrder)) 
imap(l1, ~{
  ggplot(H.Charts, outlier.shape = NA, 
         mapping = aes(x = SampleNo, y = .data[[.y]], color = SampleNo)) +
    coord_cartesian(ylim = as.numeric(c(min(.y),max(.y))))+
    geom_point(mapping = aes(x = SampleNo, y = .data[[.y]])) +
    geom_smooth(formula = y~x, mapping = aes(label = l2),parse =T, method = "lm",hjust =-0.35)+
    geom_hline(linetype = 'dashed', color = 'blue', size = 0.75,
               mapping = aes(yintercept = as.numeric(.x[[1,"hm"]]))) + 
    geom_hline(linetype = 'dashed', color = 'firebrick', size = 0.75,
               mapping = aes(yintercept = as.numeric(.x[[1,"hm"]])
                             - (as.numeric(.x[[1,"hsd"]])) * 3)) +
    geom_hline(linetype = 'dashed', color = 'firebrick', size = 0.75,
               mapping = aes(yintercept = as.numeric(.x[[1,"hm"]]) 
                             + (as.numeric(.x[[1,"hsd"]])) * 3)) +
    ggtitle(paste0(.y, "Manufacturing Assessment")) +
    theme(plot.title = element_text(hjust = 0.5),legend.position = "None") +
    xlab(label = "Sample No") +
    ylab(paste0(.y, ' values %'))
}) -> H.PlotList


它运行,但是当我尝试查看单个图表时,我收到以下错误消息 错误:美学长度必须为 1 或与数据 (24) 相同:标签

不在一系列列表中时使用单个数据帧的示例


df2 <- structure(list(Sample = c(2113, 2113, 2114, 2114, 2115, 2115, 
2116, 2116, 2117, 2117, 2118, 2118, 2119, 2119, 2120, 2120, 2121, 
2121, 2122, 2122, 2123, 2123, 2124, 2124), Rep_No = c("A", "B", 
"A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", 
"B", "A", "B", "A", "B", "A", "B", "A", "B"), Fe = c(57.24, 57.12, 
57.2, 57.13, 57.21, 57.14, 57.16, 57.31, 57.11, 57.18, 57.21, 
57.12, 57.14, 57.17, 57.1, 57.18, 57, 57.06, 57.13, 57.09, 57.17, 
57.23, 57.09, 57.1), SiO2 = c("6.85", "6.83", "6.7", "6.69", 
"6.83", "6.8", "6.76", "6.79", "6.82", "6.82", "6.8", "6.86", 
"6.9", "6.82", "6.81", "6.83", "6.79", "6.76", "6.8", "6.88", 
"6.83", "6.79", "6.8", "6.83"), Al2O3 = c("2.9", "2.88", "2.88", 
"2.88", "2.92", "2.9", "2.89", "2.87", "2.9", "2.89", "2.9", 
"2.89", "2.89", "2.88", "2.89", "2.91", "2.91", "2.91", "2.9", 
"2.9", "2.91", "2.91", "2.88", "2.86")), row.names = c(NA, -24L
), class = "data.frame")

a <- "italic(y) == \"73\" + \"-0.0074\" %.% italic(x) * \",\" ~ ~italic(r)^2 ~ \"=\" ~ \"0.158\""

p <- ggplot(data = df2, aes(x = Sample, y = Fe))+
  geom_point(mapping = aes(x = Sample, y = Fe, color = as.factor(Sample))) +
  stat_poly_eq(formula = y ~x , mapping = aes( label = a), parse = TRUE, method = "lm", hjust = -0.35 ) +
  geom_smooth(method = lm, se = FALSE) +
  geom_boxplot(mapping = aes(x = min(Sample) - 1, y = Fe)) +  
  theme(legend.position = "None") +
  labs(title = "Lab Test Order Fe", x = "Sample No", y = "Homogeneity Test Fe %") +
  scale_x_continuous(labels = c("All Data", as.integer(df2$Sample)),
                     breaks = c(min(df2$Sample)-1, df2$Sample))

imap(l1, ~{
  H.Charts %>%
    ggplot( outlier.shape = NA, 
            mapping = aes(x = as.numeric(SampleNo), y = .data[[.y]]))  +
    geom_point(mapping = aes(x = SampleNo, y = .data[[.y]],  color = factor(SampleNo))) +
    stat_poly_eq(mapping = aes(label = l2[[.y]]), parse = TRUE, method = "lm", hjust = -0.35 ) +
    geom_smooth(method = lm, se = FALSE, aes(x = as.numeric(SampleNo), y = .data[[.y]])) +
    geom_boxplot(aes(x = min(as.numeric(SampleNo))-1, y= .data[[.y]]))+
    coord_cartesian(ylim = as.numeric(c(min(.y),max(.y))))+
    geom_hline(linetype = 'dashed', color = 'blue', size = 0.75,
               mapping = aes(yintercept = as.numeric(.x[[1,"hm"]]))) + 
    geom_hline(linetype = 'dashed', color = 'firebrick', size = 0.75,
               mapping = aes(yintercept = as.numeric(.x[[1,"hm"]])
                             - (as.numeric(.x[[1,"hsd"]])) * 3)) +
    geom_hline(linetype = 'dashed', color = 'firebrick', size = 0.75,
               mapping = aes(yintercept = as.numeric(.x[[1,"hm"]]) 
                             + (as.numeric(.x[[1,"hsd"]])) * 3)) +
    ggtitle(paste0(.y, " Manufacturing Assessment")) +
    theme(plot.title = element_text(hjust = 0.5),legend.position = "None")+
    xlab(label = "Sample No") +
    ylab(paste0(.y, ' values %'))
}) -> H.PlotList
H.PlotList[[1]]

【问题讨论】:

  • 我已经发现了几个问题,但最主要的是geom_smooth(formula = y~x, mapping = aes(label = l2),parse =T, method = "lm",hjust =-0.35) 这一行。你确定这是正确的吗?它发出警告Ignoring unknown parameters: parse, hjust 3: Ignoring unknown aesthetics: label
  • 问题是您将列表l2 映射到label。也许你想要label=l2[[.y]]。然而,正如@RonakShah 已经指出的那样,label aes 对geom_smooth 没有任何意义。如果您想添加注释,请尝试使用 geom_text 或 `annotate。
  • @RonakShah 不,我不确定它是否正确。我在该部分代码中尝试做的是从第二个列表 l2 中获取标签以匹配正确的元素名称,但是,该方法适用于单个元素并将 l2 替换为单个测试字符串
  • @RonakShah 我已经包含了一个带有单个数据框的工作示例

标签: r dataframe ggplot2 tidyverse purrr


【解决方案1】:

感谢社区的帮助,我有了最终的解决方案

imap(l1, ~{
  H.Charts %>%
    ggplot( outlier.shape = NA, 
            mapping = aes(x = as.numeric(SampleNo), y = .data[[.y]]))  +
    geom_point(mapping = aes(x = SampleNo, y = .data[[.y]],  color = factor(SampleNo))) +
    stat_poly_eq(mapping = aes(label = l2[[.y]]), parse = TRUE, method = "lm", hjust = -0.35 ) +
    geom_smooth(method = lm, se = FALSE, aes(x = as.numeric(SampleNo), y = .data[[.y]]), fullrange = FALSE) +   
    geom_boxplot(aes(x = ("All data"), y= .data[[.y]]))+
    coord_cartesian(ylim = as.numeric(c(min(.y),max(.y))))+
    geom_hline(linetype = 'dashed', color = 'blue', size = 0.75,
               mapping = aes(yintercept = as.numeric(.x[[1,"hm"]]))) + 
    geom_hline(linetype = 'dashed', color = 'firebrick', size = 0.75,
               mapping = aes(yintercept = as.numeric(.x[[1,"hm"]])
                             - (as.numeric(.x[[1,"hsd"]])) * 3)) +
    geom_hline(linetype = 'dashed', color = 'firebrick', size = 0.75,
               mapping = aes(yintercept = as.numeric(.x[[1,"hm"]]) 
                             + (as.numeric(.x[[1,"hsd"]])) * 3)) +
    ggtitle(paste0(.y, " Manufacturing Assessment")) +
    theme(plot.title = element_text(hjust = 0.5),legend.position = "None")+
    xlab(label = "Sample No") +
    ylab(paste0(.y, ' values %'))
}) -> H.PlotList
H.PlotList[[1]]

【讨论】:

    【解决方案2】:

    好吧,我将 SampleNo 更改为数字,应用 geom_smooth 很奇怪,其中 x 轴由 factor 组成。另外,将color 的位置移动到geom_point 的内部。如果此结果不符合您的目的,请告诉我。

    编辑:

    我更改了as.numeric 的位置以使其与上图相似。

    imap(l1, ~{
      H.Charts %>%
        ggplot( outlier.shape = NA, 
                mapping = aes(x = as.numeric(SampleNo), y = .data[[.y]]))  +
        geom_point(mapping = aes(x = SampleNo, y = .data[[.y]],  color = factor(SampleNo))) +
        stat_poly_eq(formula = y ~x , mapping = aes( label = l2[[.y]]), parse = TRUE, method = "lm", hjust = -0.35 ) +
        geom_smooth(method = lm, se = FALSE, aes(x = as.numeric(SampleNo), y = .data[[.y]])) +
        geom_boxplot(aes(x = factor(" All data"), y= .data[[.y]]))+
        coord_cartesian(ylim = as.numeric(c(min(.y),max(.y))))+
        geom_hline(linetype = 'dashed', color = 'blue', size = 0.75,
                   mapping = aes(yintercept = as.numeric(.x[[1,"hm"]]))) + 
        geom_hline(linetype = 'dashed', color = 'firebrick', size = 0.75,
                   mapping = aes(yintercept = as.numeric(.x[[1,"hm"]])
                                 - (as.numeric(.x[[1,"hsd"]])) * 3)) +
        geom_hline(linetype = 'dashed', color = 'firebrick', size = 0.75,
                   mapping = aes(yintercept = as.numeric(.x[[1,"hm"]]) 
                                 + (as.numeric(.x[[1,"hsd"]])) * 3)) +
        ggtitle(paste0(.y, "Manufacturing Assessment")) +
        theme(plot.title = element_text(hjust = 0.5),legend.position = "None") +
        xlab(label = "Sample No") +
        ylab(paste0(.y, ' values %'))
    }) -> H.PlotList
    H.PlotList[[1]]
    

    【讨论】:

    • 它看起来几乎是正确的。我唯一的 cmets 是我希望 x 轴标签(样本号)与图表上的点对齐。我想要一个箱形图作为图表的开头,但我的示例代码没有包含它。
    • @Spooked 我会看看它并再次回复。 :D
    • @Spooked 我在上面编辑代码和绘图。请检查一下。此外,您的问题中的情节可能会与 hlines 的更改截距相似。
    • 我已经进一步调整了您的编辑以将趋势线与箱形图分开,但松开了标签“所有数据”我尝试使用 scale_x_continuous 以使标签沿底部正确但没有成功。 scale_x_continuous(labels = c("所有数据", as.integer(df$SampleNo)), breaks = c(min(df$SampleNo)-1, df$SampleNo))
    • @Spooked 尝试将 geom_smoothgeom_boxplot 更改为 geom_smooth(method = lm, se = FALSE, aes(x = as.numeric(SampleNo), y = .data[["Fe"]]), fullrange = FALSE) + geom_boxplot(aes(x = ("All data"), y= .data[["Fe"]]))。但它会将箱线图移到最后
    猜你喜欢
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    • 2014-01-03
    • 2020-01-01
    • 1970-01-01
    • 2018-05-20
    相关资源
    最近更新 更多