I would like to create a sub grouping in a ggplot2 (geom_point), meaning that I would like to shift discrete x values slightly according to a subgroup (see Figure).
I could achieve that by changing the discrete values to continuous and add a subgroup dependent shift value (see Fig.B), and than manually adjust the x labels. But I thought there is probably a more elegant way which deals with spacing and labeling issues.Below is a minimal example which hopefully describes what I mean.
library(ggplot2)
set.seed(1)
df <- data.frame(
ID = rep(seq(1,8),2),
group = rep(LETTERS[1:4],4),
subgroup = c(rep("a",8),rep("b",8)),
value = runif(16)
)
df$xpos <- as.numeric(df$group)+(as.numeric(df$subgroup)/4)
ggplot(data=df, aes(x=group, y= value, color=subgroup))+
geom_point()+
ggtitle("How it is")
ggplot(data=df, aes(x=xpos, y= value, color=subgroup))+
geom_point() +
ggtitle("How I would like it (without adjusted xAxes Labels)")

