如何在 1 个图中用 CI 绘制 2 个平均线图
How to draw a 2 mean line plots with CI in 1 plot
我正在尝试使用 CI 在一个图中绘制 2 条均值线。我使用了具有 plotmeans
功能的 gplots
来画一条线,但我只能在一个图中画一条线。有人可以帮忙吗?非常感谢
这是我的数据示例
Group MC MB
G1 5 10
G2 8 8
G3 14 7
G4 20 6
G1 10 15
G2 16 13
G3 30 9
G4 25 7
G1 15 29
G2 20 22
G3 25 20
G4 35 15
我想将 MC 和 MB 绘制为一条线,其值为平均值,每组 CI。 xlab
将是 group
。 ylab
是 MC
和 MB
的值。
我期待这样的事情
非常感谢。
这可以通过结合使用 tidyr、dplyr 和 ggplot2 来完成。
# Your data
group <- c("G1", "G2", "G3", "G4", "G1", "G2", "G3", "G4", "G1", "G2", "G3", "G4")
mc <- c(5, 8, 14, 20, 10, 16, 30, 25, 15, 20, 25, 35)
mb <- c(10, 8, 7, 6, 15, 13, 9, 7, 29, 22, 20, 15)
df <- data.frame(group, mc, mb)
首先,我们将您的数据收集为包含类别 (cat) 和值列的长格式。
# Requires library "tidyr"
library(tidyr)
# Gathering data
df_gathered <- gather(df, cat, value, 2:3)
接下来计算组和类别的每个组合的均值和误差:
# Requires the library "dplyr"
library(dplyr)
# Calculating mean and error by group
df_mean_by_group <- df_gathered %>%
group_by(group, cat) %>%
summarise(mean = mean(value), error = qnorm(0.975)*sd(value)/length(value))
最后我们按类别对线进行分组(以连接它们),我们也按类别给它们着色:
# Requires "ggplot2"
library(ggplot2)
# Plotting it all
ggplot(df_mean_by_group, aes(x=group, y=mean, colour=cat, group=cat)) +
geom_errorbar(aes(ymin=mean-error, ymax=mean+error), colour="black", width=.1) +
geom_line() +
geom_point(size=3)
我正在尝试使用 CI 在一个图中绘制 2 条均值线。我使用了具有 plotmeans
功能的 gplots
来画一条线,但我只能在一个图中画一条线。有人可以帮忙吗?非常感谢
这是我的数据示例
Group MC MB
G1 5 10
G2 8 8
G3 14 7
G4 20 6
G1 10 15
G2 16 13
G3 30 9
G4 25 7
G1 15 29
G2 20 22
G3 25 20
G4 35 15
我想将 MC 和 MB 绘制为一条线,其值为平均值,每组 CI。 xlab
将是 group
。 ylab
是 MC
和 MB
的值。
我期待这样的事情
非常感谢。
这可以通过结合使用 tidyr、dplyr 和 ggplot2 来完成。
# Your data
group <- c("G1", "G2", "G3", "G4", "G1", "G2", "G3", "G4", "G1", "G2", "G3", "G4")
mc <- c(5, 8, 14, 20, 10, 16, 30, 25, 15, 20, 25, 35)
mb <- c(10, 8, 7, 6, 15, 13, 9, 7, 29, 22, 20, 15)
df <- data.frame(group, mc, mb)
首先,我们将您的数据收集为包含类别 (cat) 和值列的长格式。
# Requires library "tidyr"
library(tidyr)
# Gathering data
df_gathered <- gather(df, cat, value, 2:3)
接下来计算组和类别的每个组合的均值和误差:
# Requires the library "dplyr"
library(dplyr)
# Calculating mean and error by group
df_mean_by_group <- df_gathered %>%
group_by(group, cat) %>%
summarise(mean = mean(value), error = qnorm(0.975)*sd(value)/length(value))
最后我们按类别对线进行分组(以连接它们),我们也按类别给它们着色:
# Requires "ggplot2"
library(ggplot2)
# Plotting it all
ggplot(df_mean_by_group, aes(x=group, y=mean, colour=cat, group=cat)) +
geom_errorbar(aes(ymin=mean-error, ymax=mean+error), colour="black", width=.1) +
geom_line() +
geom_point(size=3)