繁体   English   中英

使用ggplot进行单个而不是多个boxplots

[英]single instead multiple boxplots with ggplot

我想根据两个因素(Tiefe)和(Ort)为变量(Theta..vol ..)制作箱线图。

 > str(data) 
  'data.frame': 30 obs. of  6 variables:  
 $ Nummer      : int   > 1 2 3 4 5 6 7 8 9 10 ... 
 $ Name        : int  11 12 13 14 15 16 17 18 19 20 ...
 $ Ort         : Factor w/ 2 levels "NNW","S": 2 2 2 2 2 2 2 2 2 2 ...
 $ Tiefe       : int  20 20 20 20 20 50 50 50 50 50 ... 
 $ Gerät       : int  2 2 2 2 2 2 2 2 2 2 ...  
 $ Theta..vol..: num  15 16.4 14.9 16.6 10.6 22.1 17.6 10 18 20.3 ...

我的代码是:

ggplot(data, aes(x = Tiefe, y = Theta..vol.., fill=Ort))+geom_boxplot()

由于变量(Tiefe)具有3个级别,变量(Ort)具有2个级别,因此我希望看到三个成对的箱型图(每个成对表示一个(Tiefe)。但是,我只能看到一对(箱级图表示“ Ort”和“ Ort”第二级的另一个箱线图,我应该如何更改才能为每个“ Tiefe”获得三对?谢谢

在您的代码中, Tiefe被读取为整数而不是一个因素。

使用dplyrggplot2轻松修复:

首先,我做了一些虚拟数据:

library(dplyr)

data <- tibble(
      Ort = ifelse(runif(30) > 0.5, "NNW", "S"),
      Tiefe = rep(c(20, 50, 75), times = 10),
      Theta..vol.. = rnorm(30,15))

接下来,我们修改Tiefe管道进入前柱ggplot

data %>% 
  mutate(Tiefe = factor(Tiefe)) %>%
  ggplot(aes(x = Tiefe, y = Theta..vol.., fill = Ort)) + 
  geom_boxplot()

ggplot

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM