簡體   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