[英]How to plot multiple boxplots with numeric x values properly in ggplot2?
[英]How do I create boxplots in ggplot2 by subsetting a column into ranges to get the X values?
我試圖通過將一列中的數據子集化為 x 然后使用第二列作為 y 值來創建箱線圖。
Spot Vol Spot_Tot_Int Spot_Max_Int Spot_Background Spot_Int/Bkg Spot_IntMax/Bkg Spot_Int-Bkg Spot_Z_Pos Spot_X_Pos Spot_Y_Pos
1 47 14757 488 47.58763 310.1016 10.254766 12520.38 4 27 79
2 46 13197 409 46.24423 285.3761 8.844346 11069.77 4 49 936
3 47 17838 573 66.40580 268.6211 8.628765 14716.93 4 63 844
4 38 12484 527 57.01034 218.9778 9.243938 10317.61 4 125 942
5 45 15113 604 43.97189 343.6969 13.736049 13134.27 4 134 891
6 40 13684 578 52.34335 261.4277 11.042473 11590.27 4 204 434
我試圖將 Spot_Z_Pos 用作 X,但將其分解為 3 個范圍(1-10、11-20、21-30),而不是為每個單獨的值 1-30 繪制一個圖。 我希望 y 值為 Spot_IntMax/Bkg。 我可以通過創建三個單獨的子集數據框來弄清楚如何在基本 R 中做到這一點,但類似的方法在 ggplot 中對我沒有幫助。
謝謝你的幫助!!
嗨,您可以為該組創建一個新變量,然后通過該變量對繪圖進行分面。 對於條形圖,這與此類似(您可以更改為 geom 層中的箱形圖):
library(dplyr)
library(ggplot2)
df %>%
dplyr::mutate(GROUP = case_when(Spot_Z_Pos < 11 ~ 1,
Spot_Z_Pos < 21 ~ 2,
Spot_Z_Pos < 31 ~ 3,
TRUE ~ 4)) %>%
ggplot2::ggplot(aes(Spot_Z_Pos, `Spot_IntMax/Bkg`)) +
ggplot2::geom_col() +
ggplot2::facet_wrap( ~ GROUP)
請注意,我為每個不小於 31 的內容創建了第 4 組,以防萬一列中出現意外情況。 還要注意,有更緊湊的功能可以分割成組......我個人更喜歡 case_when 如果 bin 的數量很少
您還可以在構建繪圖之前過濾特定組並省略 facet_wrap 行 - 這將只導致一組繪圖
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.