3

I am working on creating a forest plot for a meta analysis using ggplot2. Here is what an earlier version/incomplete graph looked like.

My problem is now that I'm going to back to work on it some more, everything works except I cant get the error bars with geom_errorbarh.

Data: Here is a data file from dput on my dropbox. For the life of me, I can't figure out how to get it to paste direct on here in a way that would copy-and-paste into R without having problems. Anyway, if you just save that file (q.df) and run df.forest <- dget("your-path/q.df"), you'll have a usable df. (Note: this is parsed down from the full df that I'm working with to only include the variables needed for this particular plot, but if something doesn't make sense, just let me know and I'll try to clarify.)

So this much works and produces the graph I want, except the error bars.

ggplot(df.forest, aes(y=ID, x=g,  shape = iv.domain, size = plot.weight))+
 geom_point(aes(size = iv.domain))+
 scale_shape_manual(name = "IV Domain",
                 values = c(15, 16, 17, 5),
                 labels = c("Abstraction", "Social", "Temporal", 
 "Summary\neffects"))+
 ylab("Refrence")+
 scale_x_continuous(limits=c(-1.5,2.5), name='Effect size (g)')+
 geom_vline(xintercept=0, color='dark grey', linetype='dotted')+
 facet_grid(dv.level~., scales= 'free', space='free')+
 theme_bw()+
 theme(panel.grid.major=element_blank(),
    panel.grid.minor=element_blank(),
    panel.border=element_blank(),
    axis.line=element_line(),
    text=element_text(family='Times'),
    plot.caption=element_text(hjust = 0, vjust = -7),
    plot.margin = unit(c(.25,.25,.75,.25), "cm"))+
 guides(size = FALSE) +
 scale_size_discrete(range = c(1.5,4.5))

But if I add: + geom_errorbarh(aes(xmin=gLL, xmax=gUL),height=.3), (where gLL and gUL are numeric values representing the 95% CI lower/upper limits), it gives me an error:
Error: Continuous value supplied to discrete scale.

However, if I enter factorized versions of gLL and gUL: + geom_errorbarh(aes(xmin=gLL.f, xmax=gUL.f),height=.3), it give me the opposite error:
Error: Discrete value supplied to continuous scale.

I've searched high and low, and can't seem to find where anyone has had problems with a function saying something is supposed to be discrete when you enter it as continuous, but saying something is supposed to be continuous when you enter it as discrete.

Moreover, I don't have a clue why it worked yesterday, yet won't work today. I've restarted R/cleared my environment, and gone back through my whole script line by line to make sure nothing leading up to this error was off, but I can't find anything. I've tried adding the aes() from geom_errorbarh to the ggplot(aes()) instead, but that doesn't change anything either. And again, if I run these lines without geom_errorbarh, it makes the exact graph I want minus the error bars, so the error is definitely in that line.

Any help at all would be greatly appreciated!

Cheers!

Edit: Thanks to @eipi10 I figured it out. I had overlooked where I had changed what my size aes() variable was (and didn't think about it since it ran just fine until you added in the geom_errorbarh line). That also explains why it was making me use scale_size_discrete which wasn't making sense, but it worked, so I went with it. Anyway, once I deleted the aes() within geom_point(), and changed scale_size_discrete to scale_size, it worked!! Thank you! -AE

AEubanks
  • 33
  • 4
  • You can get some ideas for sharing a small amount of data via copying and pasting [in this answer](https://stackoverflow.com/a/5963610/2461552) for how to make a reproducible example. – aosmith Dec 04 '17 at 19:09
  • Do you get the same error if with a plot using `geom_errorbarh` as the only layer? Getting rid of all the plotting code that is extraneous to the problem may help you figure out where things are going wrong. – aosmith Dec 04 '17 at 19:13

1 Answers1

1

This is occurring because you have two size mappings, size = plot.weight in the main ggplot call and size=iv.domain in geom_point. The first is continuous and the second is discrete. You can have one or the other, but not both, and the scale_size_*** call has to match the type of variable (continuous or discrete) that you map to size.

eipi10
  • 91,525
  • 24
  • 209
  • 285