我已经非常喜欢箱形图,其中抖动点覆盖在箱线图上以表示实际数据,如下所示:
set.seed(7)
l1 <- gl(3, 1, length=102, labels=letters[1:3])
l2 <- gl(2, 51, length=102, labels=LETTERS[1:2]) # Will use this later
y <- runif(102)
d <- data.frame(l1, l2, y)
ggplot(d, aes(x=l1, y=y)) +
geom_point(position=position_jitter(width=0.2), alpha=0.5) +
geom_boxplot(fill=NA)
Run Code Online (Sandbox Code Playgroud)
(当每个框中的数据点数量非常不同时,这些特别有用.)
当我(隐含地)使用position_dodge
第二个变量分隔箱图时,我想使用这种技术,例如
ggplot(d, aes(x=l1, y=y, colour=l2)) +
geom_point(position=position_jitter(width=0.2), alpha=0.5) +
geom_boxplot(fill=NA)
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何通过colour
变量(此处l2
)躲避点,并且还会抖动它们.
这是一种手动执行抖动和躲避的方法.
# a plot with no dodging or jittering of the points
dp <- ggplot(d, aes(x=l1, y=y, colour=l2)) +
geom_point(alpha=0.5) +
geom_boxplot(fill=NA)
# build the plot for rendering
foo <- ggplot_build(dp)
# now replace the 'x' values in the data for layer 1 (unjittered and un-dodged points)
# with the appropriately dodged and jittered points
foo$data[[1]][['x']] <- jitter(foo$data[[2]][['x']][foo$data[[1]][['group']]],amount = 0.2)
# now draw the plot (need to explicitly load grid package)
library(grid)
grid.draw(ggplot_gtable(foo))
# note the following works without explicitly loading grid
plot(ggplot_gtable(foo))
Run Code Online (Sandbox Code Playgroud)