Home > OS >  How to plot a control and a treatment groups in R?
How to plot a control and a treatment groups in R?

Time:10-24

dput (Data) is, as follows:

structure(list(Year = c(1986, 1987, 1988, 1989, 1990, 1991, 1992, 
1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001), RwandaGDP = c(266296395453522, 
266232388162044, 278209717380819, 278108075482628, 271435453185924, 
264610535380715, 280150385073342, 257433853555685, 128078318071279, 
173019272512077, 195267342948145, 222311386633263, 242005217615319, 
252537014428159, 273676681432581, 296896832706772), ChadGDP = c(221078469390513, 
215510570376333, 248876690715831, 261033657789193, 250126438514823, 
271475073131674, 293196997307684, 247136226809204, 272188148422562, 
275553889112468, 282165595568286, 297579071872462, 318265518859647, 
316009224207253, 313311638596115, 349837931311225), RwandaLifeExpectancy = c(50.233, 
47.409, 43.361, 38.439, 33.413, 29.248, 26.691, 26.172, 27.738, 
31.037, 35.38, 39.838, 43.686, 46.639, 48.649, 49.936), ChadLifeExpectancy = c(46.397, 
46.601, 46.772, 46.91, 47.019, 47.108, 47.187, 47.265, 47.345, 
47.426, 47.498, 47.559, 47.61, 47.657, 47.713, 47.789)), row.names = c(NA, 
-16L), spec = structure(list(cols = list(Year = structure(list(), class = c("collector_double", 
"collector")), RwandaGDP = structure(list(), class = c("collector_double", 
"collector")), ChadGDP = structure(list(), class = c("collector_double", 
"collector")), RwandaLifeExpectancy = structure(list(), class = c("collector_double", 
"collector")), ChadLifeExpectancy = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ";"), class = "col_spec"), problems = <pointer: 0x000001f0ef568410>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

I come from performing a Difference in Differences regression in R, with the following code:

GDP <- as.numeric(Data$RwandaGDP, Data$ChadGDP)

MyDataTime <- ifelse(Data$Year >= "1994", 1, 0)

MyDataTreated <- Data$RwandaLifeExpectancy

MyDataDiD <- MyDataTime * MyDataTreated

DidReg = lm(GDP ~ MyDataTime   MyDataTreated   MyDataDiD, data = Data)

summary(DidReg)

Now, there is only one thing left to do, which is to plot the results.

I am looking for something akin to what can be seen in point 3.4 (line plot) on this website:

enter image description here

CodePudding user response:

Am not sure why you want to use a gradient color scale when you already have the GDP represented on the y-axis. Consider something like this. This approach also sets you up to graph your other variables and multiple countries.



Rwanda <- Data %>%
    select(Year, LifeExpectancy = RwandaLifeExpectancy, GDP = RwandaGDP) %>%
    mutate(Country = "Rwanda")

Chad <- Data %>%
    select(Year, LifeExpectancy = ChadLifeExpectancy, GDP = ChadGDP) %>%
    mutate(Country = "Chad")


CountryData <- rbind(Rwanda, Chad) %>%
    mutate(`GDP(Trillions)` = round(GDP/1000000000000,2))


CountryData %>%
    ggplot(aes(x=Year))  
    geom_line(aes(y=`GDP(Trillions)`, group = Country, color = Country), size=1.2)  
    geom_vline(xintercept = 1994, linetype="dotted", 
               color = "black", size=1.1)  
    ggrepel::geom_label_repel(data = CountryData[CountryData$Year == 1994,], aes(label = `GDP(Trillions)`, y = `GDP(Trillions)`),
                              nudge_x = 0.5, nudge_y = -0.5,
                              na.rm = TRUE)  
    guides(scale="none")  
    labs(x="", y="GDP (USD Trillions)")  
    annotate(
        "text",
        x = 1994,
        y = median(CountryData$`GDP(Trillions)`),
        label = "{Difference-in-Differences}",
        angle = 90,
        size = 3
    )

enter image description here

CodePudding user response:

Try changing your scale_colour_continuous(palette = "Accent") to scale_colour_continuous(type = "gradient")

I also removed your scale_y_continous. Unsure rationale behind this code.

added pivot_longer

data %>%
  pivot_longer(cols = c("RwandaGDP","ChadGDP"), names_to = "country", values_to = "value") %>% 
  mutate(Year = as.numeric(Year),
         label = if_else(Year == "1994", as.character(value), NA_character_)) %>%
  ggplot(aes(x=Year,y=value,col=country))  
  geom_line(size=1.2)  
  # scale_color_continuous(type = "gradient")  
  geom_vline(xintercept = 1994, linetype="dotted", 
             color = "black", size=1.1)  
  # scale_color_discrete(palette = "Accent")  
  # scale_y_continuous(limits = c(17,24))  
  ggrepel::geom_label_repel(aes(label = label),
                   nudge_x = 0.5, nudge_y = -0.5,
                   na.rm = TRUE)  
  guides(scale="none")  
  labs(x="", y="GDP")  
  annotate(
    geom = "text",
    x = 1994,
    y = median(GDP),
    label = "Difference-in-Differences",
    angle = 90,
    size = 3
  )

enter image description here

  • Related