I am plotting a series of effect size estimates using the following code –
Fig1 <- ggplot(dat1, aes(x=x, y= value, colour = time, group = variable))+
geom_point(aes(shape = variable),size = 1.75, position = position_quasirandom()) +
geom_line(data=dat1[!is.na(dat1$value),], aes(colour = NULL, group = NULL)) +
scale_shape_manual(values = 0:20) +
scale_y_continuous(limits=c(-0.50,.00), breaks=seq(-0.50,.00, by = .02)) +
geom_hline(yintercept = 0, linetype=2) +
coord_flip() +
xlab('Distribution') +
ylab('Effect size')
This creates a plot that looks like this:
I’d like to adjust the code so that the plotted values are offset (above the line) at varying heights depending on how close they are to other values. For example, for the first line in this plot, the diamond and squares would remain on the line where they are currently located, but the upside-down triangle, circle, vertical line, and asterisk would be elevated off the line. It would look roughly like this:
Does anyone know if this is possible using ggplot2 or is there another R package that would allow us to do that? If so, can anyone suggest a way to adjust the code so that it bumps estimates off the line based on how close they are to other estimates?
Reproducible example:
### Installing of needed packages
if (!require("pacman")) install.packages("pacman")
library(pacman)
pacman::p_load(ggplot2, reshape2, dplyr, magrittr, ggbeeswarm, beepr, readxl)
#data
data<-data.frame(x = "Activity",
mean.b = .12,
osrmin.b = .11,
osrmax.b = .13,
osrmed.b = .12,
TFFE.b = .05,
TFRE.b = .1,
smm.b = .0,
sms.b = .07,
CMA.b = .09,
Petpeese.b = .09,
mean.a = .11,
osrmin.a = .09,
osrmax.a = .10,
osrmed.a = .10,
TFFE.a = .11,
TFRE.a = .05,
smm.a = .07,
smr.a = .09,
CMA.a = .10,
Petpeese.a = .11)
# normalize the data (i.e., convert into 'long' form)
dat1 = melt(data, id.vars = "x")
# Set x factor order in order that appears in data
dat1$x = factor(dat1$x, levels = unique(dat1$x))
dat1 %<>% mutate(time = gsub(".*\\.", "", variable),
variable = gsub("\\..*", "", variable))
# Begin plotting
Fig1 <- ggplot(dat1, aes(x=x, y= value, colour = time, group = variable))+
geom_point(aes(shape = variable),size = 1.75) +
geom_line(data=dat1[!is.na(dat1$value),], aes(colour = NULL, group = NULL)) +
scale_shape_manual(values = 0:20) +
scale_y_continuous(limits=c(0,.2), breaks=seq(0,.2, by = .02)) +
geom_hline(yintercept = 0, linetype=2) +
coord_flip() +
xlab('Distribution') +
ylab('Effect size')
Fig1 <- Fig1 +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.x = element_line(colour = "black"),
axis.line.y = element_line(colour = "black"),
axis.text.x = element_text(size = 6),
axis.text.y = element_text(size = 6),
axis.title = element_text(size=6),
legend.position = ("none"),
legend.title=element_blank(),
legend.text=element_text(size=6),
legend.key = element_rect(colour = NA))
Fig1 + guides(colour = guide_legend(nrow = 3, byrow = TRUE)) +
guides(shape=guide_legend(ncol=3,byrow=TRUE))
Fig1