[英]Grouped ggplot boxplot in R
對於示例數據框:
df <- structure(list(year = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L), letter_group = c("A", "A", "A", "B", "B", "B", "C",
"C", "C", "C", "A", "A", "A", "B", "B", "B", "C", "C", "C", "C",
"A", "A", "A", "B", "B", "B", "C", "C", "C", "C", "C", "C", "C",
"A", "A", "A", "B", "B", "B", "C", "C", "C", "C", "C"), value = c(2L,
3L, 4L, 5L, 6L, 6L, 7L, 8L, 5L, 6L, 7L, 3L, 4L, 5L, 6L, 4L, 5L,
6L, 2L, 3L, 4L, 4L, 5L, 6L, 7L, 8L, 5L, 3L, 2L, 4L, 5L, 6L, 4L,
3L, 4L, 5L, 6L, 7L, 1L, 2L, 4L, 5L, 6L, 4L)), .Names = c("year",
"letter_group", "value"), row.names = c(NA, -44L), class = c("tbl_df",
"tbl", "data.frame"), spec = structure(list(cols = structure(list(
year = structure(list(), class = c("collector_integer", "collector"
)), letter_group = structure(list(), class = c("collector_character",
"collector")), value = structure(list(), class = c("collector_integer",
"collector"))), .Names = c("year", "letter_group", "value"
)), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
我正在嘗試創建一個包含x軸年份的箱形圖 - 以及按年份分組的'字母組'...
即A,B,C為第1年,然后是小空間A,BC,第2年等等....
我有以下內容:
library(ggplot2)
p1 <- ggplot(df, aes(year, value))
p1 + geom_boxplot(aes(group=letter_group))
但這只是產生3箱圖。
有人可以幫幫我嗎?
@ nouse解決方案的替代方案(這是最好的解決方案)是使用分面。 但是,分面的一個好處是您還可以在x軸上獲得字母組標簽。
# Load library
library(ggplot2)
# Define data frame
df <- structure(list(year = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L), letter_group = c("A", "A", "A", "B", "B", "B", "C",
"C", "C", "C", "A", "A", "A", "B", "B", "B", "C", "C", "C", "C",
"A", "A", "A", "B", "B", "B", "C", "C", "C", "C", "C", "C", "C",
"A", "A", "A", "B", "B", "B", "C", "C", "C", "C", "C"),
value = c(2L, 3L, 4L, 5L, 6L, 6L, 7L, 8L, 5L, 6L, 7L, 3L, 4L, 5L, 6L, 4L, 5L,
6L, 2L, 3L, 4L, 4L, 5L, 6L, 7L, 8L, 5L, 3L, 2L, 4L, 5L, 6L, 4L,
3L, 4L, 5L, 6L, 7L, 1L, 2L, 4L, 5L, 6L, 4L)),
.Names = c("year", "letter_group", "value"),
row.names = c(NA, -44L),
class = c("tbl_df","tbl", "data.frame"),
spec = structure(list(cols = structure(list( ear = structure(list(), class = c("collector_integer", "collector")),
letter_group = structure(list(), class = c("collector_character", "collector")),
value = structure(list(), class = c("collector_integer", "collector"))),
.Names = c("year", "letter_group", "value")),
default = structure(list(), class = c("collector_guess", "collector"))),
.Names = c("cols", "default"), class = "col_spec"))
# Plot results
g <- ggplot(df)
g <- g + geom_boxplot(aes(letter_group, value))
g <- g + facet_grid(. ~ year, switch = "x")
g <- g + theme(strip.placement = "outside",
strip.background = element_blank(),
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(colour = alpha("gray50", 0.25), linetype = "dashed"))
g <- g + ylab("Value") + xlab("Year & Letter Group")
print(g)
由reprex包創建於2019-05-23(v0.2.1)
您的問題已在很大程度上回答了這里 。
您的數據框不包含因素,因此您首先需要將分組變量轉換為因子。 然后,根據上面給出的鏈接,有兩個選項。 通過組合兩個原始因子構建一個新因子(如z-cool的答案中所示) - 但這不會在x軸上創建因子水平之間的所需空間 - 或者您需要指定一個因子來fill
或者col
。 在您的情況下,解決問題的最快方法是
ggplot(df, aes(as.factor(year), value, fill=as.factor(letter_group))) + geom_boxplot()
如果您不想為您的繪圖着色,可以使用scale_fill_manual
或scale_color_manual
更改此scale_color_manual
,具體取決於您之前在aes
的選擇:
ggplot(df, aes(as.factor(year), value, fill=as.factor(letter_group))) + geom_boxplot() +
scale_fill_manual(values=c("white", "white", "white")) +
theme(legend.position = "none")
這應該工作
library(tidyverse)
df %>%
mutate(year_group = paste(year, letter_group)) %>%
ggplot(aes(year_group, value)) +
geom_boxplot()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.