Search code examples
rggplot2graphcustomization

ggplot lines not through shapes


I want to create a line plot with the shapes varied by the Methods variable in my dataset. However, I don't want the lines to be drawn over the shapes, for example this is not good: enter image description here

How do I hide the lines behind the shapes so that the lines are not through the shapes. Here is the dataset:

xx <- data.frame(
  stringsAsFactors = FALSE,
               rho = c(0.7,0.7,0.7,0.7,0.7,0.7,
                       0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,
                       0.7,0.7,0.7,0.7,0.7,0.7),
            sample = c(1L,1L,1L,1L,1L,1L,1L,1L,
                       2L,2L,2L,2L,2L,2L,2L,2L,3L,3L,3L,3L,3L,3L,
                       3L,3L),
                tp = c(10L,10L,10L,10L,20L,20L,
                       20L,20L,10L,10L,10L,10L,20L,20L,20L,20L,10L,10L,
                       10L,10L,20L,20L,20L,20L),
           Methods = c("lmm","residualboot",
                       "clustboot","mbbboot","lmm","residualboot","clustboot",
                       "mbbboot","lmm","residualboot","clustboot","mbbboot",
                       "lmm","residualboot","clustboot","mbbboot","lmm",
                       "residualboot","clustboot","mbbboot","lmm","residualboot",
                       "clustboot","mbbboot"),
      fixinterbias = c(-0.07069111,-0.08709062,
                       -0.13675904,-0.03077662,-0.2093937,-0.2092973,0.2344589,
                       -0.1650586,-0.08666544,-0.09681292,0.05795378,
                       -0.08564713,-0.015873476,-0.022712667,-0.090171359,
                       0.001930576,0.03720186,0.04073916,-0.08692844,0.04538355,
                       -0.09867106,-0.09874304,-0.08654507,-0.1161617),
      fixslopebias = c(0.06225352,0.06467038,
                       0.06003106,0.05557157,-0.01036622,-0.01039492,-0.083628,
                       -0.01530608,0.02736118,0.02863767,0.04872466,0.02607667,
                       0.08056533,0.08076664,0.09773794,0.07819871,
                       -0.0703907,-0.07103784,-0.06005637,-0.07246422,0.0189303,
                       0.01863365,0.0145846,0.02057585)
)

And here is my code:

fixinter <- ggplot(xx, aes(x=sample, y=fixinterbias, shape=Methods, linetype=Methods)) +
  geom_line(aes(color=Methods), size = 0.75) +
  geom_hline(yintercept=0, linetype="dashed", color = "black") +
  scale_shape_manual(values=c(0, 1, 2, 5)) +
  scale_x_continuous(name="Sample size (n)", breaks = c(1, 2, 3), label = c(20, 50, 100)) +
  scale_y_continuous(name="fix-effect intercept bias") +
  geom_point(aes(color=Methods, shape = Methods), 
             stroke = 1.0, fill = "white") +
  theme_classic()

fixinter + facet_grid(tp ~. )

Solution

  • You can use the "filled" version of each shape (use for example ggpubr::show_point_shapes() to see a list), so here 22, 21, 24 and 23.

    fixinter <- ggplot(xx, aes(x=sample, y=fixinterbias, shape=Methods, linetype=Methods)) +
      geom_line(aes(color=Methods), size = 0.75) +
      geom_hline(yintercept=0, linetype="dashed", color = "black") +
      scale_shape_manual(values=c(22, 21, 24, 23)) +
      scale_x_continuous(name="Sample size (n)", breaks = c(1, 2, 3), label = c(20, 50, 100)) +
      scale_y_continuous(name="fix-effect intercept bias") +
      geom_point(aes(color=Methods, shape = Methods), 
                 stroke = 1.0, fill = "white") +
      theme_classic()
    
    fixinter + facet_grid(tp ~. )
    

    enter image description here