在for循环中组合多个ggplot几何 [英] Combining multiple ggplot geoms in a for-loop

查看:70
本文介绍了在for循环中组合多个ggplot几何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项调查的定量数据集.我想为我拥有的值(最小lb,最大ub和模式ml)绘制拟合三角形分布.请注意,我正在使用rtriang(),因为我的数据不包含可以拟合密度函数的分位数.至少那是我的理解.

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.

这段代码返回许多单独的图形.我想将每个i(或响应者)的geom_density对象显示在一个图中.我将如何实现呢?

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="_"))
    }
  }
}

推荐答案

我不确定我是否完全理解.您在找这个吗?

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 %")
               })

@Tjebo建议的另一种方法

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 %")

这篇关于在for循环中组合多个ggplot几何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆