我正在试着制作一个有4个盒子图的图。两个用于Word测试: Us和非Us,两个用于简单测试:Us和非Us。我正在尝试将它们合并在一起,这样它们看起来就像这样:
What I want the graph to look like
我为每个测试创建了两个ggplot,但我想不出一种方法来将它们合并在一起。这是我使用的tibble。请注意,Q65用于简单测试,而Q66用于单词测试。
# A tibble: 86 x 5
Q65 Q66 nationality race_ethnicity type
<dbl> <dbl> <chr> <chr> <dbl>
1 55.1 51.2 Mexican Mexican 2
2 62.7 38.7 American Asian 1
3 53.2 34.7 USA Hispanic 1
4 193. 84.6 chinese asian 2
5 57.0 40.9 Taiwan Asian 2
6 103. 58.5 American caucasian 1
7 49.2 35.5 White White 2
8 213. 2135. Chinese Asian 2
9 85.6 52.1 Chinese Chinese 2
10 168. 113. China Asian 2
# ... with 76 more rows
以下是ggplots的代码:
EasyTestPlot <-
ggplot(Qtibble, aes(as.factor(Qtibble$type), Q65, color = factor(type))) + geom_boxplot(
alpha = 0.5, outlier.colour = "black", outlier.fill = "black", outlier.shape = 21, outlier.size =
2 ) + coord_cartesian(ylim = c(0, 250)) + labs(x = "Easy Test", y = "Number of Seconds to
Compare") + theme_light() + theme(axis.text.x = element_text(face = "bold", size = 10, angle =0),
axis.text.y = element_text(face = "bold", size = 10, angle = 0)) + scale_x_discrete(labels = c("1" = "US", "2" = "Non-US")) + stat_summary(fun = mean, color = "darkred", position = position_dodge(0.75), geom = "point", shape = 18, size = 3, show.legend = FALSE) + scale_color_manual(values=c("Dark green", "red")) + labs(colour="Nationality",linetype="Nationality",shape="Nationality") + theme(legend.text = element_text(color = "white"),legend.title = element_text(color = "white"), legend.key = element_rect(fill = "white"))
+ scale_color_discrete(guide =
guide_legend(override.aes = list(color = "white")))
和
WordTestPlot <-
ggplot(Qtibble, aes(as.factor(Qtibble$type), Q66, color = factor(type))) + geom_boxplot(
alpha = 0.5, outlier.colour = "black", outlier.fill = "black", outlier.shape = 21, outlier.size =
2 ) + coord_cartesian(ylim = c(0, 250)) + labs(x = "Word Test", y = "Number of Seconds to
Compare") + theme_light() + theme(axis.text.x = element_text(face = "bold", size = 10, angle =0),
axis.text.y = element_text(face = "bold", size = 10, angle = 0)) + scale_x_discrete(labels = c("1" = "US", "2" = "Non-US")) + stat_summary(fun = mean, color = "darkred", position = position_dodge(0.75), geom = "point", shape = 18, size = 3, show.legend = FALSE) + scale_color_manual(values=c("Dark green", "red")) + labs(colour="Nationality",linetype="Nationality",shape="Nationality") + theme(legend.text = element_text(color = "white"),legend.title = element_text(color = "white"), legend.key = element_rect(fill = "white"))
+ scale_color_discrete(guide =
guide_legend(override.aes = list(color = "white")))
另外,我使用的是grid.arrange,但我希望它出现在一个图中,而不是并排:
grid.arrange(WordTestPlot ,EasyTestPlot, nrow = 1, ncol =2)
What the graphs currently look like
我该怎么做才能让盒子图看起来像第一张图片呢?
发布于 2020-11-20 23:10:21
我们可以将数据重塑为'long‘格式,然后应用ggplot
library(dplyr)
library(tidyr)
library(ggplot2)
Qtibble_long <- Qtibble %>%
pivot_longer(cols = Q65:Q66, names_to = 'Q6566') %>%
mutate(type = factor(type)) %>%
unite(typenew, Q6566, type)
现在使用
ggplot(Qtibble_long, aes(typenew, value, color = typenew)) +
geom_boxplot(alpha = 0.5, outlier.colour = "black",
outlier.fill = "black", outlier.shape = 21, outlier.size =
2) +
coord_cartesian(ylim = c(0, 250))
发布于 2020-11-20 23:27:29
您需要的是一种刻面方法。为此,您可以使用facet_wrap()
,ggplot2
中的常见数据结构是像其他用户一样使用整形后的数据。这里是多面图的代码。df
是您的数据:
library(dplyr)
library(tidyr)
library(ggplot2)
#Code
df %>% pivot_longer(-c(nationality,race_ethnicity,type)) %>%
ggplot(aes(x=factor(type),y=value,color = factor(type)))+
geom_boxplot(alpha = 0.5, outlier.colour = "black",
outlier.fill = "black", outlier.shape = 21,
outlier.size =2)+coord_cartesian(ylim = c(0, 250))+
labs(x = "Easy Test", y = "Number of Seconds to
Compare") +
theme_light() +
theme(axis.text.x = element_text(face = "bold", size = 10, angle =0),
axis.text.y = element_text(face = "bold", size = 10, angle = 0))+
scale_x_discrete(labels = c("1" = "US", "2" = "Non-US"))+
stat_summary(fun = mean, color = "darkred",
position = position_dodge(0.75),
geom = "point",
shape = 18, size = 3,
show.legend = FALSE) +
scale_color_manual(values=c("Dark green", "red")) +
labs(colour="Nationality",linetype="Nationality",shape="Nationality") +
theme(legend.text = element_text(color = "white"),
legend.title = element_text(color = "white"),
legend.key = element_rect(fill = "white"))+
facet_wrap(.~name,scales = 'free',strip.position = 'bottom')+
theme(panel.grid = element_blank(),
strip.placement = 'outside',
strip.background = element_blank(),
strip.text = element_text(color='black',face='bold'))
输出:
如果只希望有一个y轴,可以用facet_wrap(.~name,scales = 'free_x',strip.position = 'bottom')
替换小平面直线。
https://stackoverflow.com/questions/64938095
复制相似问题