Reputation: 89
I'm trying to create side-by-side plots with nudged data points (Odds ratios, with 95% CI error bars) in R using ggplot. Each time I try to combine them I get an error. Can anyone help me identify what I should do to change my code? This is the error I get:
Error in (~surv) + scale_x_continuous(breaks = seq(0, 4, 1)) : non-numeric argument to binary operator
To illustrate what I'm trying to do, see below a version I created using plot(), which you can see is fairly ugly: I've tried combining the facet_wrap and position_nudge based on the guidance in the J Stuart Carlton blog, but haven't been able to add a position_nudge. The error code above suggests that the problem is with facet_wrap section of my code.
I've included code below describing how to replicate my dataset.
activity <- factor(rep(c("Good interaction", "Poor interaction",
"RTW plan"), times = 4))
surv <- factor(rep(c("T1", "T2"), each = 3, times = 2))
mod <- factor(rep(c("Crude", "Adjusted"), each = 6))
or <- c(1.72, 1.26, 2.39, 2.5, 1.34, 1.89, 1.14, 1.09, 2.02, 1.9, 1.1, 1.02)
low <- c(1.22, 0.74, 1.73, 1.74, 0.61, 1.35, 0.77, 0.61, 1.40, 1.22, 0.60, 0.68)
hi <- c(2.41, 2.16, 3.29, 3.6, 1.8, 2.64, 1.70, 1.94, 2.90, 2.95, 2.04, 1.54)
rtwc <- data.frame(activity, surv, mod, or, low, hi)
And here is the ggplot code I've been using:
ggplot(rtwc, aes(x = or, y = activity, colour = mod)) +
geom_vline(aes(xintercept = 1), size = 0.25, linetype = "dashed") +
geom_errorbarh(data = filter(rtwc, mod == "crude"), aes(xmax = hi, xmin = low), size = 0.5, height = 0.1, colour = "gray50", position = position_nudge(y = fix)) +
geom_point(data = filter(rtwc, mod == "crude"), aes(xmax = hi, xmin = low), size = 4, position_nudge(y = fix)) +
geom_errorbarh(data = filter(rtwc, mod == "Adjusted"), aes(xmax = hi, xmin = low), size = 0.5, height = 0.1, colour = "gray50", position = position_nudge(y = -fix)) +
geom_point(data = filter(rtwc, mod == "Adjusted"), size = 4, position = position_nudge(y = -fix)) +
geom_errorbarh(data = filter(rtwc, mod = "Adjusted")) +
facet_wrap = (~surv) +
scale_x_continuous(breaks = seq(0, 4, 1)) +
coord_trans(x = "log10") +
theme_bw() +
theme(panel.grid.minor = element_blank())
Apologies if there is already a post on this question.
Upvotes: 1
Views: 997
Reputation: 29125
Here are some code to get you started. I used position_dodge
together with coord_flip
to keep the error bar pairs away from each other:
ggplot(rtwc,
aes(y = or, ymin = low, ymax = hi, x = activity, group = mod)) +
geom_hline(yintercept = 1, size = 0.25, linetype = "dashed", colour = "grey") +
geom_errorbar(width = 0.2, position = position_dodge(0.5)) +
geom_point(aes(col = mod), position = position_dodge(0.5),
size = 3) +
scale_x_discrete(name = "") +
scale_y_log10(name = "", breaks = seq(0, 4)) +
scale_color_manual(name = "", values = c("red", "blue")) + # change colours here
expand_limits(y = c(0.1, 4)) + #adjust x axis range here
facet_wrap(~surv) +
coord_flip() +
theme_bw() + #change look & feel here
theme(panel.grid.minor = element_blank())
For tweaks to the plot's look & feel, you can check out the available themes in ggplot
here, & more themes in ggthemes
here. Just please don't use the Excel 2013 theme. As the creator noted, its presence is for ironic purposes only.
For tweaks to the point colours, here's a handy reference for colours by name. Or you can use one of the palettes from RColorBrewer
, viewable via RColorBrewer::display.brewer.all()
.
Upvotes: 2