简体   繁体   English

如何在 R 中绘制多个条件的图?

[英]How to draw plots for multiple conditions in R?

I am new to R studio and I am trying to draw bar plots for a specific problem, which has multiple conditions.我是 R 工作室的新手,我正在尝试为具有多个条件的特定问题绘制条形图。 the problem is:问题是:

A producer guarantees that germinability of one special pea cultivar is 50%.生产者保证一个特殊豌豆品种的发芽率为50%。 A gardener bought 50 pea seeds.一位园丁买了 50 颗豌豆种子。 Calculate the probability that:计算以下概率:

  1. all seeds will sprout,所有的种子都会发芽,
  2. at most 5 seeds will sprout,最多发5颗种子,
  3. at least 4 seeds will sprout.至少有 4 颗种子会发芽。
  4. and so on...等等...

Now, how can I draw bar plots for these?现在,我怎样才能为这些绘制条形图? I am currently having this solution:我目前有这个解决方案:

xP50 <- c(0:50)
prob1 <- dbinom(xP50,50,0.5)
tab1 <- data.frame(value = xP50, probability = prob1)
barplot(tab1$probability)

and for the second one, I have:对于第二个,我有:

xP5 <- c(0:50)
prob2 <- dbinom(xP5,50,0.5)
tab2 <- data.frame(value = xP5, probability = prob2)
barplot(tab2$probability)

Is this the right way, to achieve the bar plots?这是实现条形图的正确方法吗? Am I doing it correctly?我做得对吗?

Thank You.谢谢你。

prob=dbinom(50, 50, .5)
barplot(c(prob, 1-prob), names.arg=c("Sprout", "Not Sprout"), main="All 50 Seeds")

在此处输入图像描述

prob=pbinom(5, 50, .5)
barplot(c(prob, 1-prob), names.arg=c("Sprout", "Not Sprout"), main="At Most 5 Seeds")

在此处输入图像描述

prob=pbinom(3, 50, .5, lower.tail=FALSE)
barplot(c(prob, 1-prob), names.arg=c("Sprout", "Not Sprout"), main="At Least 4 Seeds")

在此处输入图像描述

xP50 <- c(0:50)
prob1 <- dbinom(xP50,50,0.5)
tab1 <- data.frame(x = xP50, probability = prob1)
library(ggplot2)
ggplot(tab1) + geom_histogram(aes(x, probability), stat="identity") + ggtitle("Probability of x pea seeds being germinable")

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM