Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 是否有办法使用ggplot2在箱线图上输入beta发行版?_R_Ggplot2 - Fatal编程技术网

R 是否有办法使用ggplot2在箱线图上输入beta发行版?

R 是否有办法使用ggplot2在箱线图上输入beta发行版?,r,ggplot2,R,Ggplot2,我试图创建一个方框图,其中y轴是从csv数据表创建的beta发行版,而x轴是来自同一数据表的列。该表称为beta,下面是它的dput: structure(list(weld.type.ID = 1:33, weld.type = structure(c(29L, 11L, 16L, 4L, 28L, 17L, 19L, 5L, 24L, 27L, 21L, 32L, 12L, 20L, 26L, 25L, 3L, 7L, 13L, 22L, 33L, 1L, 9L, 10L, 18L, 1

我试图创建一个方框图,其中y轴是从csv数据表创建的beta发行版,而x轴是来自同一数据表的列。该表称为beta,下面是它的dput:

structure(list(weld.type.ID = 1:33, weld.type = structure(c(29L, 
11L, 16L, 4L, 28L, 17L, 19L, 5L, 24L, 27L, 21L, 32L, 12L, 20L, 
26L, 25L, 3L, 7L, 13L, 22L, 33L, 1L, 9L, 10L, 18L, 15L, 31L, 
8L, 23L, 2L, 14L, 6L, 30L), .Label = c("1,40,Material A", "1,40S,Material C", 
"1,80,Material A", "1,STD,Material A", "1,XS,Material A", "10,10S,Material C", 
"10,160,Material A", "10,40,Material A", "10,40S,Material C", 
"10,80,Material A", "10,STD,Material A", "10,XS,Material A", 
"13,40,Material A", "13,40S,Material C", "13,80,Material A", 
"13,STD,Material A", "13,XS,Material A", "14,40,Material A", 
"14,STD,Material A", "14,XS,Material A", "15,STD,Material A", 
"15,XS,Material A", "2,10S,Material C", "2,160,Material A", "2,40,Material A", 
"2,40S,Material C", "2,80,Material A", "2,STD,Material A", "2,XS,Material A", 
"4,80,Material A", "4,STD,Material A", "6,STD,Material A", "6,XS,Material A"
), class = "factor"), a = c(281L, 196L, 59L, 96L, 442L, 98L, 
66L, 30L, 68L, 43L, 35L, 44L, 23L, 14L, 24L, 38L, 8L, 8L, 5L, 
19L, 37L, 38L, 6L, 11L, 29L, 6L, 16L, 6L, 16L, 3L, 4L, 9L, 12L
), b = c(7194L, 4298L, 3457L, 2982L, 4280L, 3605L, 2229L, 1744L, 
2234L, 1012L, 1096L, 1023L, 1461L, 1303L, 531L, 233L, 630L, 502L, 
328L, 509L, 629L, 554L, 358L, 501L, 422L, 566L, 403L, 211L, 159L, 
268L, 167L, 140L, 621L)), row.names = c(NA, -33L), class = "data.frame")
我尝试的第一个代码是:

SampleValuesBox <- rbeta(10000,beta$a,beta$b)
SampleValuesBoxPlot <- data.frame(yvalue = SampleValuesBox)

my_boxplot <- ggplot(beta, aes(x = weld.type.ID, y = yvalue)) + 
  geom_boxplot()+
  labs(x="Weld Type ID", y="repair rate")

print(my_boxplot)

SampleValuesBox <- rbeta(10000,beta$a,beta$b)
ggplot(data.frame(vals=SampleValuesBox))+
  geom_boxplot(aes(x = beta$weld.type.ID, y = vals))+
  labs(x="Weld Type ID", y="repair rate")

我犯了这个错误

FUNX[[i]]中出现错误,…:找不到对象“yvalue”

我尝试的第二个代码是:

SampleValuesBox <- rbeta(10000,beta$a,beta$b)
SampleValuesBoxPlot <- data.frame(yvalue = SampleValuesBox)

my_boxplot <- ggplot(beta, aes(x = weld.type.ID, y = yvalue)) + 
  geom_boxplot()+
  labs(x="Weld Type ID", y="repair rate")

print(my_boxplot)

SampleValuesBox <- rbeta(10000,beta$a,beta$b)
ggplot(data.frame(vals=SampleValuesBox))+
  geom_boxplot(aes(x = beta$weld.type.ID, y = vals))+
  labs(x="Weld Type ID", y="repair rate")

我犯了这个错误

错误:长度必须为1或与数据相同 10000:x

我试过的第三个代码:

my_boxplot <- ggplot(beta, aes(x = weld.type.ID, y = SampleValuesBox)) + 
  geom_boxplot() + 
  labs(x="Weld Type ID", y="repair rate")

我犯了这个错误

错误:长度必须为1或与数据相同 33:y

预期结果是一个如下图:


我似乎找不到办法将beta发行版与beta数据表中的列组合到同一个方框图中。任何帮助都将不胜感激

如果将所有需要的数据字段放入一个数据帧中,并将其传递到ggplot中,那么ggplot2的使用寿命将变得更轻松。查看以下各项是否适用于您:

# specify number of desired yvalues for each weld.type.ID
n <- 100 

# generate yvalues from beta distribution for each weld.type.ID
SampleValuesBox <- lapply(seq(1, nrow(beta)),
                          function(i) rbeta(n, beta$a[i], beta$b[i]))

# combine generated yvalues with their associated weld.type.ID values
SampleValuesBoxPlot <- data.frame(weld.type.ID = rep(beta$weld.type.ID, each = n),
                                  yvalue = unlist(SampleValuesBox))

ggplot(SampleValuesBoxPlot, 
       aes(x = factor(weld.type.ID), y = yvalue)) + 
  geom_boxplot()+
  labs(x="Weld Type ID", y="repair rate")
我在您的示例数据上运行了droplevels,它去除了完整数据中未使用的分类级别类别,但不在本示例中。这会让你的问题更容易回答。