[英]boxplot data ggplot2 package
我是R的新人,希望你能帮助我。 我想在一张图中制作多个箱线图,但我无法像这样获得 output:
这是我自己的数据:
我使用了这个命令:
library(tidyverse)
library(readxl)
library(ggplot2)
marte <- read_xlsx("marterstudio.xlsx")
head(marte)
marte <- gather(marte, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "O", "P", key="ID", value="value")
marte$Group <- as.factor(marte$Group)
marte$ID <- as.factor(marte$ID)
ggplot(marte, aes(x = value, y = ID, color = ID)) +
geom_boxplot()
这是结果:
你能帮助我吗?
您需要的是coord_flip
function 而不是像您那样分配 x/y。
ggplot(data = marte) +
# I can put the data param in ggplot call but rather define the aes
# inside the geom_ call this allow you specify different aes for
# different geom if you happened to use multiple geom in one plot
geom_boxplot(aes(x = ID, y = value, color = ID)) +
coord_flip()
制作每组只有 1 或 2 个值的箱线图可能会误导每个总体的真实方差。 但只是为了演示代码,您可以执行以下操作:
# load necessary packages
library(tidyverse)
# to reproduce sampling rows
set.seed(1)
# produce boxplot (not recommended for small samples)
iris %>%
select(Species, Sepal.Length, Sepal.Width) %>%
pivot_longer(-Species) %>%
group_by(Species, name) %>%
sample_n(size = 2, replace = FALSE) %>%
ggplot(aes(x = name, y = value, fill = Species)) +
geom_boxplot() +
coord_flip()
产生这个 plot:
在实践中,当样本量相当小(例如 n < 10)时,显示单个数据点的信息量更大,或许可以使用一些汇总统计数据,例如平均值或中位数。 以下是我更倾向于用样本大小 = 2 来表示数据的方式:
# to reproduce sampling rows
set.seed(1)
# produce bar plot with overlaid points (recommended for small samples)
iris %>%
select(Species, Sepal.Length, Sepal.Width) %>%
pivot_longer(-Species) %>%
group_by(Species, name) %>%
sample_n(size = 2, replace = FALSE) %>%
ggplot(aes(x = name, y = value, fill = Species)) +
stat_summary(fun = mean, geom = "bar", position = "dodge") +
geom_point(shape = 21, size = 3, position = position_dodge(width = 0.9)) +
coord_flip()
这给出了这个 plot:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.