# A Little R in R

As usually happens when learning something new, it gets pointed out that while i might have achieved my goal, there is a far cooler way of doing the same thing! In this case that would be R. Now it seems a shame to have got this far into a career without ever having played around in R. The above is pretty close to the simplest plot posssible, but hey, it was fun!

And my notes (to self) on how to make the above:

```> library(readxl)
> Nowcasting_Zahlen <- read_excel("Nowcast/Nowcasting_Zahlen.xlsx",  +     sheet = "Nowcast_R") > library(ggplot2)
> names(Nowcasting_Zahlen)
 "Datum des Erkrankungsbeginns"
 "Punktschätzer der Anzahl Neuerkrankungen (ohne Glättung)"
 "Untere Grenze des 95%-Prädiktionsintervalls der Anzahl Neuerkrankungen (ohne Glä"
 "Obere Grenze des 95%-Prädiktionsintervalls der Anzahl Neuerkrankungen (ohne Glät"
 "Punktschätzer der Anzahl Neuerkrankungen"
 "Untere Grenze des 95%-Prädiktionsintervalls der Anzahl Neuerkrankungen"
 "Obere Grenze des 95%-Prädiktionsintervalls der Anzahl Neuerkrankungen"
 "Punktschätzer der Reproduktionszahl R"
 "Untere Grenze des 95%-Prädiktionsintervalls der Reproduktionszahl R"
 "Obere Grenze des 95%-Prädiktionsintervalls der Reproduktionszahl R"
 "Punktschätzer des 7-Tage-R Wertes"
 "Untere Grenze des 95%-Prädiktionsintervalls des 7-Tage-R Wertes"
 "Obere Grenze des 95%-Prädiktionsintervalls des 7-Tage-R Wertes"
> names(Nowcasting_Zahlen) <- c("date","neu","neuunter","neuobere","neu2", "neu2unter", "neu2obere", "R", "Runter", "Robere", "R7", "R7unter", "R7obere")
> ggplot(data = Nowcasting_Zahlen, mapping = aes(x=date, y=R))
+ geom_line(color = "firebrick")
+ geom_ribbon(data = Nowcasting_Zahlen, mapping = aes(ymin=Runter, ymax=Robere), alpha=0.3)
+ geom_line(mapping = aes(date, R7), color="darkblue")
+ geom_ribbon(data = Nowcasting_Zahlen, mapping = aes(x=date, y=R7, ymin=R7unter, ymax=R7obere), alpha=0.4)
+ ylab("R") + xlab("Date")
+ ggtitle(label = "Reproduction Number for Germany", subtitle = "Red - 4 day average, Blue - 7 day average")```

That last part is especially ugly and needs to be rewritten with more sensible syntax, to at least break out the individual pieces. Oh, and come up with more sensible names… couldn’t work out how to reference the columns by index. That would help!

The more sensible syntax looks like this:

```> g <- ggplot(data = Nowcasting_Zahlen)
> g <- g + geom_line(mapping = aes(x=date, y=R), color = "firebrick")
> g <- g + geom_ribbon(mapping = aes(x=date, y=R, ymin=Runter, ymax=Robere), alpha = 0.3)
> g <- g + geom_line(mapping = aes(x=date, y=R7), color = "darkblue")
> g <- g + geom_ribbon(mapping = aes(x=date, y=R7, ymin=R7unter, ymax=R7obere), alpha = 0.5)
> g <- g + ylab("R") + xlab("Date")
> g <- g + ggtitle(label = "Reproduction Number for Germany", subtitle = "Red - 4 day average, Blue - 7 day average")
> g```

My expectation would be that the “data” could be defined once for the “ggplot” and the “geom_ribbon” could inherit “mapping” (and add to it?) from the “geom_line”, so perhaps this can be smaller yet?