多面ggplot中的ggstatsplot字幕输出 [英] ggstatsplot subtitle output in faceted ggplot
问题描述
我正在尝试将 grouped_ggbetweenstats()的输出添加到多面ggplot中.这个
我们想要的是这样的东西(多面除外):
所以问题是:
- 是否可以获取方面标签来解释mtext()指令 OR
- 是否可以在绘图环境之外解释mtext()指令,然后将此解释后的文本作为标签传递给facet_wrap/grid OR
- 是否可以在与标签分开的方面包含字幕(如 stat_compare_mean 一样) OR
- 其他...
您只需要在构面标签中解析这些表达式,就可以这样做:
set.seed(123)库(ggplot2)库(ggstatsplot)# 数据mtcars1<-mtcars统计信息<-grouped_ggbetweenstats(数据= mtcars1,x =圆柱,y = mpg,grouping.var = am,输出=字幕")mtcars1 $ am<-factor(mtcars1 $ am,级别= c(0,1),标签=统计)# 阴谋mtcars1%>%ggplot(aes(x = cyl,y = mpg))+geom_jitter()+facet_wrap(vars(am),ncol = 1,strip.position ="top",labeller = ggplot2 :: label_parsed)
由 reprex软件包(v0.3.0)创建于2020-07-18 sup>
I'm trying to add the output of grouped_ggbetweenstats() to a faceted ggplot. This similar question here says that it is impossible, but I feel like the responder didn't quite understand the question.
group_ggbetweenstats() has an option output = "subtitle" that rather than producing a plot, produces a string representing the output of the statistical test which is to be interpreted by grDevices::mtext(). This text can then be provided to the factor you are faceting by as a label. The only part of the puzzle that seems to be missing is the evaluation by grDevices.
Example below:
library(ggplot2)
library(ggstatsplot)
mtcars1 <- mtcars
statistics <- grouped_ggbetweenstats(data = mtcars1, x = cyl, y = mpg, grouping.var = am, output = "subtitle")
mtcars1$am <- factor(mtcars1$am, levels = c(0,1), labels = statistics)
mtcars1 %>%
ggplot(aes(x = cyl, y = mpg)) +
geom_jitter() +
facet_wrap(vars(am), ncol =1, strip.position = "top")
This gives:
Whereas what we want is something that looks like this (except faceted):
So the questions are:
- Is it possible to get facet labels to interpret mtext() instructions OR
- Is it possible to interpret mtext() instructions outside of a plot environment and then pass this interpreted text as labels to facet_wrap/grid OR
- Is it possible to include a subtitle to facets separate to the label (like stat_compare_mean does) OR
- Other...
You just need to parse those expressions in the facet labels and this should do it:
set.seed(123)
library(ggplot2)
library(ggstatsplot)
# data
mtcars1 <- mtcars
statistics <- grouped_ggbetweenstats(data = mtcars1, x = cyl, y = mpg, grouping.var = am, output = "subtitle")
mtcars1$am <- factor(mtcars1$am, levels = c(0,1), labels = statistics)
# plot
mtcars1 %>%
ggplot(aes(x = cyl, y = mpg)) +
geom_jitter() +
facet_wrap(vars(am), ncol =1, strip.position = "top", labeller = ggplot2::label_parsed)
Created on 2020-07-18 by the reprex package (v0.3.0)
这篇关于多面ggplot中的ggstatsplot字幕输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!