我正在尝试用箱形图和点重建半个半图。 理想情况下,我希望在箱形图的左侧订购点,而不是重叠。 到目前为止,我已经按照这个 -
https://github.com/h21k/R/blob/master/snippets/half_box.R
但我删除了第29行的as.numeric函数。
是否可以将我的点移到他的情况下左边,这样它们就不会与盒子图重叠,或者更好的是将它们排列在每个盒子旁边的水平直方图中?
我希望这很清楚。
脚本中的相关代码:
library(ggplot2);library(ggthemes)
ggplot(iris) +
theme_stata() +
theme(line = element_blank()) +
stat_boxplot(aes(x = Species, y = Sepal.Length), geom='errorbar', linetype=1, width=0.2) +
geom_boxplot(aes(x = Species, y = Sepal.Length, fill=Species),
alpha = 1, size = 0.75, width = 0.25, outlier.shape = 3) +
annotate("rect", xmin = 1, xmax = 1.5, ymin = 0, ymax = 8, alpha = 1, fill = 'white') +
annotate("rect", xmin = 2, xmax = 2.5, ymin = 0, ymax = 8, alpha = 1, fill = 'white') +
annotate("rect", xmin = 3, xmax = 3.5, ymin = 0, ymax = 8, alpha = 1, fill = 'white') +
geom_point(aes(x = as.numeric(Species) + 0.1, colour = Species, y = Sepal.Length),
alpha = 0.5, position = position_jitter(width = 0.1))
答案 0 :(得分:0)
我不完全确定这是不是你想要的,但你可以这样做,将它们排列在另一个之上:
require(tidyverse)
boxplot <- ggplot(iris) +
theme_stata() +
theme(line = element_blank(), legend.position="none") +
stat_boxplot(aes(x = Species, y = Sepal.Length), geom='errorbar', linetype=1, width=0.2) +
geom_boxplot(aes(x = Species, y = Sepal.Length, fill=Species),
alpha = 1, size = 0.75, width = 0.25, outlier.shape = 3)
histogram <- ggplot(iris) +
theme_stata() +
theme(line = element_blank()) +
geom_histogram(aes(Sepal.Length, fill=Species),
alpha = 1, size = 0.75) +
facet_grid(~Species)
ggpubr::ggarrange(boxplot, histogram, nrow = 2)