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

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

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

问题

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

mean_value_VO2 <- 250.57
min_value_VO2 <- 128.16
sd_value_VO2 <- 351.74
Proportion (%) under the curve=63.6%

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

我尝试了以下代码:

# 创建x值的向量
x <- seq(min_value_VO2, mean_value_VO2 + 2*sd_value_VO2, length = 27)

# 使用正态分布函数计算y值
y <- dnorm(x, mean = mean_value_VO2, sd = sd_value_VO2)

# 绘制曲线
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:

mean_value_VO2 &lt;- 250.57
min_value_VO2 &lt;- 128.16
sd_value_VO2 &lt;- 351.74
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

# Create a vector of x values
x &lt;- seq(min_value_VO2, mean_value_VO2 + 2*sd_value_VO2, length = 27)

# Calculate the y values using the normal distribution function
y &lt;- dnorm(x, mean = mean_value_VO2, sd = sd_value_VO2)

# Plot the curve
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包来生成您的图表:

library(ggplot2)

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

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

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

# 指定数据集以及分配每个变量到哪个轴
ggplot(data = df, aes(x = x, y = y)) + 
  # 添加线条
  geom_line() +
  # 添加用于超过最小值的值的区域,并设置透明度为50%
  geom_area(data = df[df$x >= min_value_VO2, ], alpha = 0.5) +
  # 添加最小值的线段
  geom_segment(aes(x = min_value_VO2, xend = min_value_VO2,
                   y = 0, yend = y[match(round(min_value_VO2), x)])) +
  # 添加平均值的线段
  geom_segment(aes(x = mean_value_VO2, xend = mean_value_VO2,
                   y = 0, yend = y[match(round(mean_value_VO2), x)]), 
               linetype = "dashed") +
  # 添加平均值和标准差数值的注释
  annotate("text", x = mean_value_VO2, y = 0.00125,
           label = paste("Mean =", mean_value_VO2, "\n", "SD =", sd_value_VO2)) +
  # 添加最小值的注释
  annotate("text", x = min_value_VO2, y = -0.0001, label = paste("Min =", min_value_VO2)) + 
  # 添加曲线下方的比例注释
  annotate("text", x = 450, y = 0.0005, label = "Proportion:\n 63%") +
  # 应用"void"预设主题
  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:

library(ggplot2)

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

df &lt;- data.frame(x = seq(-800, 1300, 1),
                 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:

# Indicate the dataset and to which axis assign each variable
ggplot(data = df, aes(x = x, y = y)) + 
  # Add a line
  geom_line() +
  # Add the area for values above the min with 50% opacity
  geom_area(data = df[df$x &gt;= min_value_VO2, ], alpha = .5) +
  # Add the segment for the min value
  geom_segment(aes(x = min_value_VO2, xend = min_value_VO2,
                   y = 0, yend = y[match(round(min_value_VO2), x)])) +
  # Add the segment for the mean value
  geom_segment(aes(x = mean_value_VO2, xend = mean_value_VO2,
                   y = 0, yend = y[match(round(mean_value_VO2), x)]), 
               linetype = &quot;dashed&quot;) +
  # Add the annotation for the mean/sd values
  annotate(&quot;text&quot;, x = mean_value_VO2, y = 0.00125,
           label = paste(&quot;Mean =&quot;, mean_value_VO2, &quot;\n&quot;, &quot;SD =&quot;, sd_value_VO2)) +
  # Add the annotation for the min value
  annotate(&quot;text&quot;, x = min_value_VO2, y = -.0001, label = paste(&quot;Min =&quot;, min_value_VO2)) + 
  # Add the annotation for the proportion under the curve
  annotate(&quot;text&quot;, x = 450, y = .0005,label = &quot;Proportion:\n 63%&quot;) +
  # Apply the &quot;void&quot; preset theme
  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:

确定