How to make a plot showing mean, SD, min and % based on a normal distribution in R language?

huangapple go评论74阅读模式
英文:

How to make a plot showing mean, SD, min and % based on a normal distribution in R language?

问题

基于具有以下参数的正态分布:

  1. mean_value_VO2 <- 250.57
  2. min_value_VO2 <- 128.16
  3. sd_value_VO2 <- 351.74
  4. Proportion (%) under the curve=63.6%

我希望制作一张类似于论文中第6图的图表,链接在 "https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5985399/#SM1" 中。

我尝试了以下代码:

  1. # 创建x值的向量
  2. x <- seq(min_value_VO2, mean_value_VO2 + 2*sd_value_VO2, length = 27)
  3. # 使用正态分布函数计算y值
  4. y <- dnorm(x, mean = mean_value_VO2, sd = sd_value_VO2)
  5. # 绘制曲线
  6. plot(x, y, type = "l", xlab = "X轴标签", ylab = "Y轴标签")

但结果不如预期。特别是,我希望标记上述所有数字(均值=250.57,最小值=128.16和标准差=351.74;以及曲线下面积=63.6%)。

因此,非常感谢任何帮助。谢谢你!

英文:

Based on a normal distribution with the following parameters:

  1. mean_value_VO2 &lt;- 250.57
  2. min_value_VO2 &lt;- 128.16
  3. sd_value_VO2 &lt;- 351.74
  4. Proportion (%) under the curve=63.6%

I wish to make a plot as Figure 6 in the paper at "<https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5985399/#SM1>".

I tried the following codes

  1. # Create a vector of x values
  2. x &lt;- seq(min_value_VO2, mean_value_VO2 + 2*sd_value_VO2, length = 27)
  3. # Calculate the y values using the normal distribution function
  4. y &lt;- dnorm(x, mean = mean_value_VO2, sd = sd_value_VO2)
  5. # Plot the curve
  6. plot(x, y, type = &quot;l&quot;, xlab = &quot;X-axis label&quot;, ylab = &quot;Y-axis label&quot;)

, but it's not what as expected. In particular, I wish to label all the above numbers (mean=250.57, min=128.16 and sd=351.74; and proportion which is the area under the curve= 63.6%.

Therefore, any help would be much appreciated.

Thank you so much.

答案1

得分: 1

我建议您使用ggplot2包来生成您的图表:

  1. library(ggplot2)

首先需要在数据框中汇总xy变量:

  1. df <- data.frame(x = seq(-800, 1300, 1),
  2. y = dnorm(seq(-800, 1300, 1), mean = mean_value_VO2, sd = sd_value_VO2))

然后,您可以按照您的要求生成图形,如下所示:

  1. # 指定数据集以及分配每个变量到哪个轴
  2. ggplot(data = df, aes(x = x, y = y)) +
  3. # 添加线条
  4. geom_line() +
  5. # 添加用于超过最小值的值的区域,并设置透明度为50%
  6. geom_area(data = df[df$x >= min_value_VO2, ], alpha = 0.5) +
  7. # 添加最小值的线段
  8. geom_segment(aes(x = min_value_VO2, xend = min_value_VO2,
  9. y = 0, yend = y[match(round(min_value_VO2), x)])) +
  10. # 添加平均值的线段
  11. geom_segment(aes(x = mean_value_VO2, xend = mean_value_VO2,
  12. y = 0, yend = y[match(round(mean_value_VO2), x)]),
  13. linetype = "dashed") +
  14. # 添加平均值和标准差数值的注释
  15. annotate("text", x = mean_value_VO2, y = 0.00125,
  16. label = paste("Mean =", mean_value_VO2, "\n", "SD =", sd_value_VO2)) +
  17. # 添加最小值的注释
  18. annotate("text", x = min_value_VO2, y = -0.0001, label = paste("Min =", min_value_VO2)) +
  19. # 添加曲线下方的比例注释
  20. annotate("text", x = 450, y = 0.0005, label = "Proportion:\n 63%") +
  21. # 应用"void"预设主题
  22. theme_void()

How to make a plot showing mean, SD, min and % based on a normal distribution in R language?

英文:

I recommend you to use the ggplot2 package to generate your graph:

  1. library(ggplot2)

It requires to gather the x and y variables in a dataframe first:

  1. df &lt;- data.frame(x = seq(-800, 1300, 1),
  2. y = dnorm(seq(-800, 1300, 1), mean = mean_value_VO2, sd = sd_value_VO2))

And a plot such as the one you're aiming for can be generated as follows:

  1. # Indicate the dataset and to which axis assign each variable
  2. ggplot(data = df, aes(x = x, y = y)) +
  3. # Add a line
  4. geom_line() +
  5. # Add the area for values above the min with 50% opacity
  6. geom_area(data = df[df$x &gt;= min_value_VO2, ], alpha = .5) +
  7. # Add the segment for the min value
  8. geom_segment(aes(x = min_value_VO2, xend = min_value_VO2,
  9. y = 0, yend = y[match(round(min_value_VO2), x)])) +
  10. # Add the segment for the mean value
  11. geom_segment(aes(x = mean_value_VO2, xend = mean_value_VO2,
  12. y = 0, yend = y[match(round(mean_value_VO2), x)]),
  13. linetype = &quot;dashed&quot;) +
  14. # Add the annotation for the mean/sd values
  15. annotate(&quot;text&quot;, x = mean_value_VO2, y = 0.00125,
  16. label = paste(&quot;Mean =&quot;, mean_value_VO2, &quot;\n&quot;, &quot;SD =&quot;, sd_value_VO2)) +
  17. # Add the annotation for the min value
  18. annotate(&quot;text&quot;, x = min_value_VO2, y = -.0001, label = paste(&quot;Min =&quot;, min_value_VO2)) +
  19. # Add the annotation for the proportion under the curve
  20. annotate(&quot;text&quot;, x = 450, y = .0005,label = &quot;Proportion:\n 63%&quot;) +
  21. # Apply the &quot;void&quot; preset theme
  22. theme_void()

How to make a plot showing mean, SD, min and % based on a normal distribution in R language?

huangapple
  • 本文由 发表于 2023年2月26日 19:33:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定