英文:
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 <- 250.57
min_value_VO2 <- 128.16
sd_value_VO2 <- 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 <- seq(min_value_VO2, mean_value_VO2 + 2*sd_value_VO2, length = 27)
# Calculate the y values using the normal distribution function
y <- dnorm(x, mean = mean_value_VO2, sd = sd_value_VO2)
# Plot the curve
plot(x, y, type = "l", xlab = "X-axis label", ylab = "Y-axis label")
, 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)
首先需要在数据框中汇总x
和y
变量:
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()
英文:
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 <- 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 >= 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 = "dashed") +
# Add the annotation for the mean/sd values
annotate("text", x = mean_value_VO2, y = 0.00125,
label = paste("Mean =", mean_value_VO2, "\n", "SD =", sd_value_VO2)) +
# Add the annotation for the min value
annotate("text", x = min_value_VO2, y = -.0001, label = paste("Min =", min_value_VO2)) +
# Add the annotation for the proportion under the curve
annotate("text", x = 450, y = .0005,label = "Proportion:\n 63%") +
# Apply the "void" preset theme
theme_void()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论