简体   繁体   English

R中的ggplot boxplot分组

[英]Grouped ggplot boxplot in R

For a sample dataframe: 对于示例数据框:

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

I am trying to create a box plot which comprises the years on the x axes - but also the 'letter-groups' grouped by year... 我正在尝试创建一个包含x轴年份的箱形图 - 以及按年份分组的'字母组'...

ie A, B, C for year 1, then a small space then A, BC for year 2 and so on.... 即A,B,C为第1年,然后是小空间A,BC,第2年等等....

I have the following: 我有以下内容:

library(ggplot2)

p1 <- ggplot(df, aes(year, value))
p1 + geom_boxplot(aes(group=letter_group))

But this is only producing the 3 box plots. 但这只是产生3箱图。

Could someone please help me? 有人可以帮帮我吗?

An alternative to @nouse's solution (which is the best solution) is to use faceting. @ nouse解决方案的替代方案(这是最好的解决方案)是使用分面。 One benefit of faceting, however, is that you also get letter group labels on the x-axis. 但是,分面的一个好处是您还可以在x轴上获得字母组标签。

Define data structure 定义数据结构

# 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 绘制结果

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

Created on 2019-05-23 by the reprex package (v0.2.1) reprex包创建于2019-05-23(v0.2.1)

Your question has been largely answered here . 您的问题已在很大程度上回答了这里

Your dataframe does not include factors, so you would first need to turn your grouping variables into factors. 您的数据框不包含因素,因此您首先需要将分组变量转换为因子。 Then, there are two options, as per link given above. 然后,根据上面给出的链接,有两个选项。 Either construct a new factor by combining your two original factors (as shown in z-cool's answer) - but this does not create the desired space between factor levels on the x-axis - or you would need to assign one of your factors to fill , or col . 通过组合两个原始因子构建一个新因子(如z-cool的答案中所示) - 但这不会在x轴上创建因子水平之间的所需空间 - 或者您需要指定一个因子来fill或者col In your case, the quickest way to solve your problem is 在您的情况下,解决问题的最快方法是

ggplot(df, aes(as.factor(year), value, fill=as.factor(letter_group))) + geom_boxplot()

If you do not want to colorize your plot, you can change this with scale_fill_manual or scale_color_manual , depending on your choice in aes before: 如果您不想为您的绘图着色,可以使用scale_fill_manualscale_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")

This should work 这应该工作

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.

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