英文:
Number of boxplots are inaccurate. How to fix it?
问题
请问有人能帮助我弄清楚我的ggplot2箱线图出了什么问题吗?
我的数据框如下所示:
structure(list(month = structure(1:6, levels = c("1", "2", "3", "4", "11", "12"), class = "factor"),
stat_year = c("2019/2020","2019/2020", "2019/2020", "2019/2020", "2019/2020","2019/2020"),
treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L), levels = c("Random","Animal"), class = "factor"),
temp = c(3.22324156891496, 2.78498667711599, 5.22250073313783, 10.0049242424242, 3.9333986013986, 3.16336408243376)),
row.names = c(NA, 6L), class = "data.frame")
这是我用于创建箱线图的代码:
sample.plot1 = ggplot(table1, aes(x = month, y = temp, group = treatment)) +
geom_boxplot() + theme_classic()
sample.plot1 + facet_wrap(.~stat_year, ncol = 1)
但是它不是每个月显示两个箱线图,而是整年显示两个。
我需要的是在三个冬季中每个月显示两种处理方法。
我无法弄清楚出了什么问题。
英文:
Can someone please help me figure out what is wrong with my ggplot2 boxplot?
My dataframe looks like follows:
structure(list(month = structure(1:6, levels = c("1", "2", "3", "4", "11", "12"), class = "factor"),
stat_year = c("2019/2020","2019/2020", "2019/2020", "2019/2020", "2019/2020","2019/2020"),
treatment = structure(c(1L, 1L, 1L, 1L, 1L, 1L), levels = c("Random","Animal"), class = "factor"),
temp = c(3.22324156891496, 2.78498667711599, 5.22250073313783, 10.0049242424242, 3.9333986013986, 3.16336408243376)),
row.names = c(NA, 6L), class = "data.frame")
This is the code I used to create the boxplot
sample.plot1 = ggplot(table1, aes(x = month, y = temp, group = treatment)) +
geom_boxplot() + theme_classic()
sample.plot1 + facet_wrap(.~stat_year, ncol = 1)
Instead of showing two boxplots per each month, it shows two for the whole year.
What I need is for two treatments to show up for each month over the three winters.
I cannot figure out what is wrong.
答案1
得分: 1
"group = treatment"表示您希望每个治疗数值生成一个箱子,更或者说忽略x轴。
您的示例数据不太有用,因为它只包含"Random"治疗数值,但我认为改为使用"fill = treatment"而不是"group = treatment"会起作用 - 这将按治疗数值为每个箱子上色,而且由于您不会用"group"覆盖x轴分组,所以会在每个月份数值上生成一组箱子。
英文:
The group aesthetic overrides any other aesthetics to determine which rows go into a single box. group = treatment
means you want 1 box per treatment value, more-or-less ignoring the x-axis.
Your sample data isn't very useful because it only contains "Random" treatment values, but I would think changing to fill = treatment
instead of group = treatment
would work - that will color each box by the treatment value and since you won't be overriding the x-axis grouping with group will do a set of boxes at each month value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论