0

I have the following code to produce a side by side scatter plots:

ggplot(data = dat, aes(x = xvar,y = yvar)) +
  geom_point() +
  facet_wrap(grpvar)

I would like to add reference line y=x to those paneled plots, but only go the error message with the following added code:

+ abline(intercept = 0, slope = 1)

Any advice how to do this with multiple plots in a panel? Thanks!!

Bruno Pinheiro
  • 964
  • 8
  • 20
Dwdata2b
  • 25
  • 4
  • Hi, I'm not sure if it's clear what you're asking. For code debugging please always ask with a [reproducible](https://stackoverflow.com/q/5963269/1422451) example per the [MCVE](https://stackoverflow.com/help/mcve) and [`r`](https://stackoverflow.com/tags/r/info) tag description, with the desired output. – Hack-R Jun 21 '18 at 23:32
  • Try `+ geom_abline(yintercept=0, slope=1)` instead `+ abline(...`. If you provide a reproducible example would be easier to help... – Bruno Pinheiro Jun 21 '18 at 23:34
  • Thanks Bruno! The geom_abline code worked! – Dwdata2b Jun 21 '18 at 23:59
  • To suggestions, I can't due to data sensitivity but will keep that in mind for future posting... – Dwdata2b Jun 22 '18 at 00:00
  • Even if there are security or other issues with your exact data, you can always make up or find dummy data that recreates the issue – camille Jun 22 '18 at 00:32
  • You don't need to post a sample of your data, just some dummy data that have the same structure. – Carlos Eduardo Lagosta Jun 22 '18 at 00:49
  • Great @Dwdata2b! I will put as an answer, but take in account what guys said above concerning dummy data for reproducible examples. It's always a need – Bruno Pinheiro Jun 23 '18 at 16:33
  • Thanks all! I am not used to ask/answer coding questions, it'd take some practice for sure... :). – Dwdata2b Jun 26 '18 at 17:46

1 Answers1

0

In ggplot2 geometric objects (points, lines, areas...) are inserted with a geom_ prefix.

In your case you need to use

+ geom_abline(yintercept=0, slope=1)

instead of

+ abline(...)

So your code should be

ggplot(data = dat, aes(x = xvar,y = yvar)) +
  geom_point() +
  geom_abline(yintercept=0, slope=1) +
  facet_wrap(grpvar)
Bruno Pinheiro
  • 964
  • 8
  • 20