0

我是R新手,希望你能帮助我。我想在一个图中制作多个箱线图,但我无法得到这样的输出:

箱线图的图像

这是我自己的数据:

数据

我使用了这个命令:

    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()

这是结果:

结果

你能帮助我吗?

4

2 回答 2

0

您需要的是coord_flip函数,而不是像您所做的那样分配 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()
于 2020-12-24T14:04:26.947 回答
0

制作每组只有 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()

这产生了这个情节:

箱形图

在实践中,当样本量相当小(例如 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()

这给出了这个情节:

带有重叠点的条形图

于 2020-12-25T20:15:38.250 回答