简体   繁体   English

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

[英]single instead multiple boxplots with ggplot

I would like to make a boxplot for a variable (Theta..vol..) depending on two factors (Tiefe) and (Ort). 我想根据两个因素(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 ...

My code is: 我的代码是:

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

Since the variable(Tiefe) has 3 levels and the variable (Ort) has 2 levels I wish to see three paired boxplots (each pair for a single (Tiefe). But I see just a single pair (one boxplot for one level of "Ort" and another boxplot for the second level of the "Ort" What should I change to get three pairs for each "Tiefe"? Thank you 由于变量(Tiefe)具有3个级别,变量(Ort)具有2个级别,因此我希望看到三个成对的箱型图(每个成对表示一个(Tiefe)。但是,我只能看到一对(箱级图表示“ Ort”和“ Ort”第二级的另一个箱线图,我应该如何更改才能为每个“ Tiefe”获得三对?谢谢

In your code, Tiefe is being read as an integer not a factor. 在您的代码中, Tiefe被读取为整数而不是一个因素。

Easy fix using dplyr with ggplot2 : 使用dplyrggplot2轻松修复:

First I made some dummy data: 首先,我做了一些虚拟数据:

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

Next, we modify the Tiefe column before piping into the ggplot : 接下来,我们修改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