如何按组分割蜂群图,类似于:使用 ggplot2 分割小提琴图 https://stackoverflow.com/questions/35717353/split-violin-plot-with-ggplot2
但我想得到的不是密度图,而是积分......
@axeman 在链接问题中建议的“计算密度方法”显然不起作用,因为 beeswarm 不使用密度。
#Desired output:
require(ggplot2)
require(ggbeeswarm)
my_dat <- data.frame(x = 'x', m = rep(c('a','b'),100), y = rnorm(200))
ggplot(my_dat, aes(x,y))+ geom_quasirandom(method = 'smiley')
所需的输出类似:
- 结果图被编辑为Adobe插画师,为了表明什么
我想得到...
- 中心轴上的点应该是
也向左/向右躲避,具体取决于团体。
Edit
实现我想要的更好的方法是使用method = 'pseudorandom'
而不是“笑脸”。看
分裂蜂群2 https://stackoverflow.com/questions/49763045/split-beeswarm-2.
您可以尝试以下硬编码解决方案
library(tidyverse)
# the plot
p <- ggplot(my_dat, aes(x,y,color=m))+
geom_quasirandom(method = 'smiley')
# get the layer_data(p, i = 1L)
p <- ggplot_build(p)
# update the layer data
p$data[[1]] <- p$data[[1]] %>%
mutate(x=case_when(
colour=="#00BFC4" ~ PANEL + abs(PANEL - x),
TRUE ~ PANEL - abs(PANEL - x))
)
# plot the update
plot(ggplot_gtable(p))
以更通用的方式进行操作,您可以创建一个用于切换每组 x 调整的函数
foo <- function(plot){
p <- ggplot_build(plot)
p$data[[1]] <- p$data[[1]] %>%
mutate(diff = abs(x-round(x)), # calculating the difference to the x axis position
# update the new position depending if group is even (+diff) or odd (-diff)
x = case_when(group %% 2 == 0 ~ round(x) + diff,
TRUE ~ round(x) - diff)) %>%
select(-diff)
plot(ggplot_gtable(p))
}
其他一些数据
set.seed(121)
p <- diamonds %>%
mutate(col=gl(2,n()/2)) %>%
sample_n(1000) %>%
ggplot(aes(cut,y,color= factor(col)))+
geom_beeswarm()
p
和更新的情节
foo(p)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)