Below is a working example
library(ggplot2)
set.seed(926)
df <- data.frame(expression = rnorm(900),
time = c(rnorm(300), rnorm(300, 1, 2), rnorm(300, 2,0.5)),
membership = factor(rep(1:3, each = 300)))
ggplot(df, aes(x = time, y = expression, fill = membership))
geom_point(shape=21, size = 3)
geom_rug(data = subset(df, membership ==3), sides = "b", color = "green", length = unit(1.5, "cm"))
geom_rug(data = subset(df, membership ==2), sides = "b", color = "blue", length = unit(1, "cm"))
geom_rug(data = subset(df, membership ==1), sides = "b", color = "red")
scale_y_continuous(expand = c(0.3, 0))
My hope is something like
Note that I know the options of outside = TRUE
, side = "tb"
out there. But placing all rug plots at the bottom is what I really hope for.
CodePudding user response:
geom_rug
is designed to be drawn at the margins of a plot. It's probably best to use geom_point
with a custom symbol in this case:
ggplot(df, aes(x = time, y = expression, fill = membership))
geom_point(shape=21, size = 3)
geom_point(aes(y = -as.numeric(membership) - 2.5, color = membership),
shape = "|", size = 8)
geom_hline(yintercept = -3)
theme_classic(base_size = 20)
scale_y_continuous(breaks = c(-2, 0, 2))
CodePudding user response:
I don't think the position of geom_rug()
can be easily customised. I'd recommend to use geom_segment()
instead to draw the rugs like you'd want them.
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.2
set.seed(926)
df <- data.frame(expression = rnorm(900),
time = c(rnorm(300), rnorm(300, 1, 2), rnorm(300, 2,0.5)),
membership = factor(rep(1:3, each = 300)))
# Helper variables
limits <- range(df$expression)
step <- diff(limits) * 0.1
size <- 0.45 * step
ggplot(df, aes(x = time, y = expression, fill = membership))
geom_point(shape=21, size = 3)
geom_segment(
aes(
colour = membership,
xend = time,
y = limits[1] - as.numeric(membership) * step size,
yend = limits[1] - as.numeric(membership) * step - size
)
)
Created on 2022-12-12 by the reprex package (v2.0.1)