Home > Mobile >  How to change the shapes for ggplot?
How to change the shapes for ggplot?

Time:11-29

For a Difference in Difference method, I have come up with the following data:

df <- structure(list(Class = structure(c(1L, 1L, 2L, 2L), levels = c("PovCon", 
"PovDeCon"), class = "factor"), After_2015 = structure(c(1L, 
2L, 1L, 2L), levels = c("Before 2015", "After 2015"), class = "factor"), 
    mean_VLP = c(16.5314094033954, 25.3785125225305, 22.4646340695607, 
    19.5147929056452), se_duration = c(3.72103200892531, 8.17273164333138, 
    4.03966402631034, 2.56248212580638), upper = c(23.824632140889, 
    41.39706654346, 30.382375561129, 24.5372578722257), lower = c(9.23818666590181, 
    9.35995850160102, 14.5468925779924, 14.4923279390647)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -4L), groups = structure(list(
    Class = structure(1:2, levels = c("PovCon", "PovDeCon"), class = "factor"), 
    .rows = structure(list(1:2, 3:4), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))

For the graphical presentation, I used the following codes:

ggplot(df, aes(x = After_2015, 
                          y = mean_VLP, 
                         color = Class))  
   geom_pointrange(aes(ymin = lower, ymax = upper), size = 1)   
   geom_line(aes(group = Class))

Diff and Diff

Now, as per the requirement of a journal, I would need to have everything in black and white, no color!

Hence, I would ideally like to get two different shapes for the two Class and different linetypes which connect the two corresponding data points.

I used the following code to change the lines:

ggplot(plot_data_VLP, aes(x = After_2015, 
                          y = mean_VLP, 
                          shape = Class,
                          linetype = Class))  
  geom_pointrange(aes(ymin = lower, ymax = upper), size = 1)   
  geom_line(aes(group = Class)))

How do I change the shape with upward and downward triangles?

Please help, and thank you for your time.

Diff and Diff, B/W

CodePudding user response:

To get different shapes you have to map on the shape aesthetic:

library(ggplot2)

base <- ggplot(df, aes(
  x = After_2015,
  y = mean_VLP,
  linetype = Class
))  
  geom_pointrange(aes(ymin = lower, ymax = upper, shape = Class), size = 1)  
  geom_line(aes(group = Class))
base

enter image description here

UPDATE: To get upward and downward triangles you could use a manual scale where for the shapes I use some UTF8 (enter image description here

  • Related