【问题标题】:How to build multiple boxplots for 4 variables in R如何在 R 中为 4 个变量构建多个箱线图
【发布时间】:2021-04-25 00:53:55
【问题描述】:

我需要帮助为我的数据框创建多箱图。

data = Example_document

属性(变量)= F1、F2、F3、F4、F5

有人可以解释一下我如何实现这一点并获得如下图吗?

数据文件名:TestData

$ KF1 : 数量 $ KF3 : 数量 $ KF4 : 数量 $ KF5 : 数量 $ KF6 : 数量 $ KF7 : 数量
$ KF8 : 数量 $ KF9 : 数量 $ KF10 : 数量

'''

【问题讨论】:

标签: r ggplot2 data-visualization boxplot


【解决方案1】:

当然:

也许首先使用“reshape2”库中的“melt”函数。

library(ggplot2)
library(reshape2)

# 1. I dont have your data so i created this fake one:
Your_Data <- data.frame(
  "station" = seq(1:100),
  "Month_1" = rnorm(100,25,25),
  "Month_2" = rnorm(100,30,25),
  "Month_3" = rnorm(100,35,25),
  "Month_4" = rnorm(100,12,25))

# 2. Then you could transform it into a panel using "melt":
Your_Data <- reshape2::melt(
  Your_Data,
  id.vars = "station",
  variable.name = "Month",
  value.name = "Ozone")

# 3. The plot should be something like this
ggplot(Your_Data, aes(Month, Ozone)) +
  geom_boxplot() +
  # To set labels on the plot
  labs(
      title = "Boxplot of mean ozone by month",
      x = "Month",
      y = "Means ozone in \nparts per billion") +
  # To center the plot's title 
  theme(plot.title = element_text(hjust = 0.5))

【讨论】:

    猜你喜欢
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    相关资源
    最近更新 更多