Combining multiple ggplot geoms in a for-loop

浪子不回头ぞ 提交于 2020-06-01 06:23:38

问题


I have a quantitative dataset from a survey. I want to plot fit triangular distribution for the values I have (minimum lb, maximum ub, and mode ml). Mind you, I am using rtriang() as my data does not contain quantiles to which a density function can be fitted. At least that is my understanding.

This piece of code returns a lot of separate graphs. I want to display the geom_density objects for each i (or respondent) into one graph. How would I achieve this?

scenarios <- c("s1", "s2")
questions <- c("q1", "q2")
respondents <- c("1","2","3")

data_long <- data.frame(id=c("1","2","3", "1","2","3", "1","2","3",
                               "1","2","3", "1","2","3", "1","2","3",
                               "1","2","3", "1","2","3", "1","2","3",
                               "1","2","3", "1","2","3", "1","2","3"),
                         variable=c("s1_q1_ml", "s1_q1_ml", "s1_q1_ml",
                                      "s1_q1_lb", "s1_q1_lb", "s1_q1_lb",
                                      "s1_q1_ub", "s1_q1_ub", "s1_q1_ub",
                                      "s1_q2_ml", "s1_q2_ml", "s1_q2_ml",
                                      "s1_q2_lb", "s1_q2_lb", "s1_q2_lb",
                                      "s1_q2_ub", "s1_q2_ub", "s1_q2_ub",
                                      "s2_q1_ml", "s2_q1_ml", "s2_q1_ml",
                                      "s2_q1_lb", "s2_q1_lb", "s2_q1_lb",
                                      "s2_q1_ub", "s2_q1_ub", "s2_q1_ub",
                                      "s2_q2_ml", "s2_q2_ml", "s2_q1_ml",
                                      "s2_q2_lb", "s2_q2_lb", "s2_q1_lb",
                                      "s2_q2_ub", "s2_q2_ub", "s2_q1_ub"),
                         value=c(70, 70, 70, 60, 60, 60, 80, 80, 80,
                                   70, 70, 70, 60, 60, 60, 80, 80, 80,
                                   70, 70, 70, 60, 60, 60, 80, 80, 80,
                                   70, 70, 70, 60, 60, 60, 80, 80, 80))

data_long <- setDT(data_long)

for (i in respondents) {
  for (j in scenarios) {
    for (k in questions) {
      t <- rtriang(n =100000, min=as.numeric(data_long[id==i & variable == paste(j, k, "lb", sep = "_")]$value), 
                   mode=as.numeric(data_long[id==i & variable == paste(j,k, "ml", sep = "_")]$value),
                   max=as.numeric(data_long[id==i & variable == paste(j,k, "ub", sep = "_")]$value))

      # Displaying the samples in a density plot
      plot <- ggplot() + geom_density(aes(t)) + xlim(0,100) + xlab("Probability in %")
      ggsave(plot,filename=paste(i,j,k,".png",sep="_"))
    }
  }
}

回答1:


I am not sure if I completely understand. Are you looking for this?

library(tidyverse)
library(mc2d)

temp <- data_long %>%
          separate(variable, c("scenarios", "questions", "temp"),sep = "_") %>%
          group_split(id) %>%
          map(~{
              temp <- rtriang(
                        n =100000, 
                        min = .x %>% filter(temp == 'lb') %>% pull(value),
                        mode = .x %>% filter(temp == 'ml') %>% pull(value),
                        max = .x %>% filter(temp == 'ub') %>% pull(value))
               ggplot(temp) + geom_density(aes(temp)) + 
                   xlim(0,100) + xlab("Probability in %")
               })

Another approach suggested by @Tjebo

temp <- data_long %>%
         separate(variable, c("scenarios", "questions", "temp"),sep = "_") %>%
         group_split(id) %>%
         map_df(~{
          data.frame(x = rtriang(n =100000, 
                     min = .x %>% filter(temp == 'lb') %>% pull(value),
                     mode = .x %>% filter(temp == 'ml') %>% pull(value),
                     max = .x %>% filter(temp == 'ub') %>% pull(value)))
               }, .id = "id")


ggplot(temp) + geom_density(aes(x, color = id)) + 
      xlim(0,100) + xlab("Probability in %")


来源:https://stackoverflow.com/questions/61457445/combining-multiple-ggplot-geoms-in-a-for-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!