R 在geom_箱线图上覆盖geom_点()(填充=组)?
这是我目前的代码:R 在geom_箱线图上覆盖geom_点()(填充=组)?,r,ggplot2,R,Ggplot2,这是我目前的代码: require(ggplot2) value <- rnorm(40, mean = 10, sd = 1) variable <- c(rep('A', 20), rep('B', 20)) group <- rep(c('Control', 'Disease'), 20) data <- data.frame(value, variable, group) ggplot(data, aes(x=variable, y=value)) + ge
require(ggplot2)
value <- rnorm(40, mean = 10, sd = 1)
variable <- c(rep('A', 20), rep('B', 20))
group <- rep(c('Control', 'Disease'), 20)
data <- data.frame(value, variable, group)
ggplot(data, aes(x=variable, y=value)) +
geom_boxplot(aes(fill=group)) +
geom_point(aes())
require(ggplot2)
值使用位置减淡()
进行积分,并在几何点()的aes()
内添加group=group
这里尝试将Didzis的建议应用于并非所有组都有异常值的数据集,因此点与正确的框不一致。
数据文件:
require(data.table)
需要(ggplot2)
d我不知道这是什么时候推出的,但是有一个新的(ish)功能叫做position\u jitterdodge
,它简化了这一点,不管你是否想要抖动。
用法:
您也可以尝试使用ggbeeswarm
。
这里我比较了geom\u beeswarm
和geom\u quasirandom
的输出:
library(ggbeeswarm)
library(ggplot2)
ggplot(data, aes(x=variable, y=value, fill=group)) +
geom_boxplot() +
geom_beeswarm(dodge.width=0.75) +
geom_quasirandom(dodge.width=.75, col=2)
如果其中一个组没有得分,则这不会像预期的那样起作用;对于那一组,分数将不一致。@RyanHope这是特定问题的答案。不可能将所有可能的情况都包含在一个回答中。这与他提供的数据有关,但不是他提出的一般问题的完美解决方案。我只是指出这一点,因为我最近碰到了这个问题。@RyanHope你能提供这样的数据的例子吗?因为如果我删除了一个疾病图的所有数据,它就会按照预期的那样生成。
require(data.table)
require(ggplot2)
d <- readRDS("2hxp2oms5et1ktr9hukdr539b6svq1cg.rds")
ggplot(d) +
geom_boxplot(aes(x=factor(game),y=N,fill=.group),outlier.shape=NA) +
geom_point(data=dd.sum1[,.SD[which(N %in% boxplot.stats(N)$out)],by=game][,.group:=factor(.group,levels=.groups)][,],aes(x=factor(game),y=N,color=.group,group=.group),
position=position_dodge(width=0.75),
size=.5) +
facet_grid (type~., scales="free_x", space="free_x") +
xlab("Game number") +
ylab("Count") +
scale_color_brewer("Group",palette="Set1",drop=FALSE) +
scale_fill_brewer("Group",palette="Set1",drop=FALSE) +
theme(axis.text.x=element_text(size=7),legend.position="top")
ggplot(data, aes(x=variable, y=value, fill=group)) +
geom_boxplot() +
geom_point(position=position_jitterdodge())
# or, if you dont need jittering
# geom_point(position=position_jitterdodge(jitter.width = 0, jitter.height = 0))
library(ggbeeswarm)
library(ggplot2)
ggplot(data, aes(x=variable, y=value, fill=group)) +
geom_boxplot() +
geom_beeswarm(dodge.width=0.75) +
geom_quasirandom(dodge.width=.75, col=2)