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.

RinR

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)
 [1] "Datum des Erkrankungsbeginns"                                                    
 [2] "Punktschätzer der Anzahl Neuerkrankungen (ohne Glättung)"                        
 [3] "Untere Grenze des 95%-Prädiktionsintervalls der Anzahl Neuerkrankungen (ohne Glä"
 [4] "Obere Grenze des 95%-Prädiktionsintervalls der Anzahl Neuerkrankungen (ohne Glät"
 [5] "Punktschätzer der Anzahl Neuerkrankungen"                                        
 [6] "Untere Grenze des 95%-Prädiktionsintervalls der Anzahl Neuerkrankungen"          
 [7] "Obere Grenze des 95%-Prädiktionsintervalls der Anzahl Neuerkrankungen"           
 [8] "Punktschätzer der Reproduktionszahl R"                                           
 [9] "Untere Grenze des 95%-Prädiktionsintervalls der Reproduktionszahl R"             
[10] "Obere Grenze des 95%-Prädiktionsintervalls der Reproduktionszahl R"              
[11] "Punktschätzer des 7-Tage-R Wertes"                                               
[12] "Untere Grenze des 95%-Prädiktionsintervalls des 7-Tage-R Wertes"                 
[13] "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?

Wise words...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s