0

Here is the base code, kudos to this answer:

require(RCurl)
require(foreign)
x = getURL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv")
corona = read.csv(text = x, sep =",",header = T)
Italy <- corona[corona$Country.Region=='Italy',][1,5:ncol(corona)]
library(xts)
plot(xts(unlist(Italy), 
         order.by = as.Date(sub("X", "", names(Italy)),"%m.%d.%y")),
         ylim=c(0,20000), main="Number Cov-19 Italy")

Naturally I would like the values in the y axis to be the number of cases, as in here, but instead I get the actual log base 2 if I run something like:

LogItaly <- log2(Italy[,30:ncol(Italy)])
plot(xts(unlist(LogItaly), 
         order.by = as.Date(sub("X", "", names(LogItaly)),"%m.%d.%y")),
     main=expression(paste(2^y, " - Doubling of no. cases Cov-19 ITL")), 
     ylim=c(0,16))
Antoni Parellada
  • 4,253
  • 6
  • 49
  • 114

1 Answers1

1

You can use ggplot2 to produce a similar figure with Y-axs transformed as you expected.

require(ggplot2)
ts_data=xts(unlist(Italy),order.by = as.Date(sub("X", "", names(Italy)),"%m.%d.%y"))
base_breaks<-function(n=10){
    function(x){
        axisTicks(log10(range(x,na.rm=TRUE)),log=TRUE,n=n)
    }
}
ts_dataframe=data.frame(time=index(ts_data),cases=coredata(ts_data))
p<-ggplot(data=ts_dataframe[30:dim(ts_dataframe)[1],],aes(time,cases))+
      geom_line()+
      scale_y_continuous(trans='log2',breaks=base_breaks())+
      scale_x_date(date_breaks="3 days",date_minor_breaks="1 days")+
      xlab("time")+
      ylab("cases")+
      labs(title="Number Cov-19 Italy")+
      theme_bw()

More information on converting xts to data frame, you can look at here.

mikeaalv
  • 24
  • 4
  • Can you increase the number of labels on the x and y axes? – Antoni Parellada Mar 14 '20 at 23:57
  • you can replace the `scale_y_continuous(trans='log2')+` with `scale_y_continuous(trans='log2',breaks=base_breaks())+` and add `scale_x_date(date_breaks="3 days",date_minor_breaks="1 days")+`. For more details, look at [this](https://stackoverflow.com/questions/11335836/increase-number-of-axis-ticks) and [this](https://stackoverflow.com/questions/14255533/pretty-ticks-for-log-normal-scale-using-ggplot2-dynamic-not-manual/22227846) – mikeaalv Mar 15 '20 at 22:49
  • Can you edit the post with the additional lines? Ty – Antoni Parellada Mar 15 '20 at 22:49
  • I'm getting this error message: `Error in base_breaks() : could not find function "base_breaks"`. – Antoni Parellada Mar 15 '20 at 23:00
  • add the function `base_breaks `. It is used to generate log-transformed ticks. The details can be found in the link I shared in the comments. – mikeaalv Mar 16 '20 at 00:01