0

I'm currently trying to recreate an estimated marginal means plot I created using the stats software Jamovi. Jamovi uses ggplot2 to create all its graphs and it spit out this one for me: enter image description here

The problem is that the journal I'm trying to submit my manuscript to does not publish colored images and I will need to recreate the graphs in black and white (jamovi offers a grey color palette but it's hard to differentiate between the greys).

I managed to use ggplot2 in R to recreate this so far

enter image description here

with the following code:

ggplot(data, aes(x = xvar, y = yvar)) + 
  geom_line(aes(linetype = class), size = 1, show.legend = FALSE) +
  facet_grid() +
  geom_point(aes(shape = class), size = 2.5) +
  theme(axis.text = element_text(size = 16, colour = "black"), axis.title = element_text(size=16),
        title = element_text(size = 14), legend.text = element_text(size = 14)) +
  labs(x = "", y = "", title = "") +
  xlim(c(2,4)) + ylim(c(0,1)) +
  stat_smooth(aes(x = xvar, y = yvar), method="lm", formula = y ~ x, se = FALSE, fullrange = TRUE)

I'm trying to smooth out the lines between the data points like in the first graph and also have the lines extend out beyond the current range it has (2.70 - 3.7) but I don't know how to do it in ggplot. It shouldn't be a data issue because I created the data using the data frame that jamovi's own R package gave me (which is the same data frame it uses to create its plots in ggplot). I tried following the directions listed in this question but it only gives me that blue line you see in the second graph when I use stat_smooth. When I tried to follow the instructions precisely and used this code:

ggplot(data) +
  geom_plot(aes(x = xvar, y = yvar), size = 2) +
  stat_smooth(aes(x = xvar, y = yvar), method = "lm", formula = y ~ poly(x, 2), se = FALSE) + 
  coord_cartesian(ylim = c(0,1))

I instead got the following error message:

Error in geom_plot(aes(x = xvar, y = yvar), size = 2) : 
  could not find function "geom_plot"

My three questions are thus as follows:

1) How do I smooth out the lines between the data points?

2) How do I extend the lines out to the edge of the x-axes (from say, 1 to 5) like in the first graph?

3) Is there a way to anti-alias the lines like in the first graph?

Here's some sample data (not the data I used to create the plots but similar in the same format as what was given to me by jamovi where "class" is the grouping variable),

xvar    class   yvar
2       1       0.25
2       2       0.3
2       3       0.2
2       4       0.13
3       1       0.15
3       2       0.35
3       3       0.18
3       4       0.24
4       1       0.1
4       2       0.45
4       3       0.14
4       4       0.27
ssjjaca
  • 219
  • 1
  • 6
  • For the second plot, did you mean `geom_point` rather than `geom_plot`? `geom_plot` is not a ggplot function and that's what caused the error. Add `fullrange=TRUE` inside `geom_smooth` to get each fitted curve to extend over the full range of the data. I'd also suggest adding `+ theme_bw()` to get rid of the grey background, which will make it easier to distinguish grey levels in the plotted lines. If you provide some sample data, we can provide code specifically tailored to your problem. – eipi10 Jul 26 '19 at 04:33
  • By "anti-alias" do you mean you want to plot the lines with various gray levels? If so, try `ggplot(data, aes(x = xvar, y = yvar, colour=class))`. Then add `+ scale_colour_manual(values=gray(seq(0, 0.8, length=4)))` to set the colors used for each line. – eipi10 Jul 26 '19 at 04:38
  • Correction: in my first comment "full range of the data" should be "full range of the plot panel". – eipi10 Jul 26 '19 at 04:39
  • 1
    Plots on windows often have bad aliasing, if you output to png you can use the `type = 'cairo'` option for much better anti-aliasing. – Marius Jul 26 '19 at 04:40
  • @eipi10, I just added sample data to the question. For your first question, yes that was completely my mistake! For geom_smooth, I'm still getting a blue horizontal line when I use `geom_smooth(method = "lm", fullrange=TRUE`. Not sure why. – ssjjaca Jul 26 '19 at 05:10
  • @eipi10, I meant anti-alias as in smoothing out the lines upon exporting like what Marius mentioned! – ssjjaca Jul 26 '19 at 05:11
  • Just figured it out! put the code in as an answer – ssjjaca Jul 26 '19 at 05:47

1 Answers1

0

I managed to figure it out after a few trial and error. I managed to fix it by putting in the following code:

geom_smooth(aes(group = class, linetype = class), colour = "black", method="lm", formula = y ~ poly(x,2), se=FALSE, fullrange=TRUE)

ssjjaca
  • 219
  • 1
  • 6