【问题标题】:Completing individual box plot with two rows of data, compairing positive against negative用两行数据完成单个箱线图,比较正负
【发布时间】:2018-06-28 13:15:23
【问题描述】:

我对 R 还是很陌生。请有人帮我查询一下。

如何在我的数据上完成单个箱线图?我需要将每一行的正面与负面进行比较。共有 195 列。

数据如下:(正数在负数下,所以两行最多195列,我需要对每一列进行箱线图)

                     1    2    3    4    5     6   7   etc 
Negative            1.1  2.1  2.2  3.1  5.66 8.99 5.11 etc
Positive            2.1  5.6  5.7  3.0  6.1  8.1  6.2  etc

提前致谢

【问题讨论】:

  • 每列的箱线图?每列只有两个数字。你怎么能只为两个数字绘制一个箱线图?我假设您想要一个箱线图,将正面作为一组,将负面作为另一组。请看我的回答。
  • 你好。感谢您的回复。是的,我想对正负进行箱线图。

标签: r plot boxplot


【解决方案1】:
require(tidyverse) # dplyr() and ggplot2()
# your data
data <- data.frame(negative = c( 1.1,  2.1,  2.2,  3.1,  5.66, 8.99, 5.11),
               positive = c(2.1,  5.6,  5.7,  3.0,  6.1,  8.1,  6.2))
# we gather to have one long column with values and one with the "class"
data <- data %>% gather(class) # you can name here the class column
# use ggplot to plot the data
ggplot(data, aes(y = value, x = class)) +
  geom_boxplot()

请提供可重现的示例文本时间,请参阅:How to make a great R reproducible example?

【讨论】:

  • 它有 2 列,“负”和“正”,gather 我得到了长格式。
  • 你好。非常感谢您的回复。收集功能似乎有错误。聚集(。,类)中的错误:找不到函数“聚集”
  • 你需要先安装一些包,使用:install.packages("tidyverse")
  • gather()ggplot() 是“额外”功能,基本 R 包不提供。您需要先安装它们各自的库才能使用它们。
  • @LieslCarr 请考虑接受您认为相关且有用的答案。
【解决方案2】:

您的数据框是宽格式,很难使用。我们可以先把它转成长格式。

library(tidyverse)

dat2 <- dat %>%
  rownames_to_column() %>%
  gather(Column, Value, -rowname)

之后,您可以按照 Riccardo Lavelli 的建议使用 ggplot2 包绘制数据。这里我展示了另一个选项,即来自lattice 包的bwplot 函数。

library(lattice)

bwplot(Value ~ rowname, dat2)

您也可以考虑使用基础 R boxplot 函数。

boxplot(Value ~ rowname, dat2)

您也可以考虑ggpubr 包中的ggboxplot

library(ggpubr)

ggboxplot(dat2, x = "rowname", y = "Value")

数据

dat <- read.table(text = "                     1    2    3    4    5     6   7 
Negative            1.1  2.1  2.2  3.1  5.66 8.99 5.11
Positive            2.1  5.6  5.7  3.0  6.1  8.1  6.2",
                  header = TRUE)

【讨论】:

  • 谢谢。原始数据有195列,有没有办法不用手动输入就可以输入?
  • @LieslCarr 我的解决方案不需要任何手动输入。所以应该没问题。
  • 感谢您的帮助。
  • 很高兴为您提供帮助。如果它解决了您的问题,请考虑接受该帖子作为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
  • 2021-06-12
  • 2021-12-08
  • 2020-11-21
  • 2021-10-29
相关资源
最近更新 更多