Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

r - How to tailor the x-axis of a ggplot to include dates

How to insert dates in the x axis of a ggplot as this:

enter image description here

Thanks to the help from SO, I managed to have a tailored x-axis in the basic plotting function in R:

enter image description here

However, the key two lines of code used to generate the second plot:

at1 <- seq(min(ccw$date), max(ccw$date), by=1+.02*length(ccw$date))
axis.Date(1, at=at1, format="%b %d", las=2, cex.axis=0.7)

don't seem easy to adapt to ggplot, where instead I am stuck with just the number of the day. By the way, if you are about to generate the plot with a fix to address my question and can figure out a way to make the confidence bands broader in general, I would appreciate it.

Here is the entire code to generate both plots as are:

install.packages('RCurl')
install.packages('zoo')
suppressPackageStartupMessages({
    require(repr) # Enables resizing of the plots.
    require(RCurl)
    require(foreign)
    require(tidyverse) # To tip the df from long row of dates to cols (pivot_longer())
    require(zoo) # Rolling average
    require(RColorBrewer)
})

x = getURL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")
corona <- read.csv(textConnection(x))

corona = (read_csv(x)
          %>% pivot_longer(cols = -c(`Province/State`, `Country/Region`, Lat, Long),
                           names_to = "date",
                           values_to = "cases")
          %>% select(`Province/State`,`Country/Region`, date, cases)
          %>% mutate(date=as.Date(date,format="%m/%d/%y"))
          %>% drop_na(cases)
          %>% rename(country="Country/Region", provinces="Province/State")
)
 
cc <- (corona
       %>% filter(country %in% c("Italy", "Spain","US", "Norway", "Denmark", "Sweden","Korea, South", "Brazil","India", "United Kingdom", "Mexico"))
)
 
ccw <- (cc
        %>% pivot_wider(names_from="country",values_from="cases")
        %>% filter(Italy>5)
)

x = getURL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")
corona <- read.csv(textConnection(x))

corona = (read_csv(x)
          %>% pivot_longer(cols = -c(`Province/State`, `Country/Region`, Lat, Long),
                           names_to = "date",
                           values_to = "cases")
          %>% select(`Province/State`,`Country/Region`, date, cases)
          %>% mutate(date=as.Date(date,format="%m/%d/%y"))
          %>% drop_na(cases)
          %>% rename(country="Country/Region", provinces="Province/State")
)
 
cc <- (corona
       %>% filter(country %in% c("Italy", "Spain","US", "Norway", "Denmark", "Sweden","Korea, South", "Brazil","India", "United Kingdom", "Mexico"))
)
 
ccw <- (cc
        %>% pivot_wider(names_from="country",values_from="cases")
        %>% filter(Italy>5)
)


options(repr.plot.width=13, repr.plot.height=8)

ccw$first.der <- c(NA, diff(ccw$US))  ## better add an NA and integrate in data frame
ccw$day <- seq_along(ccw$date)

first.der <- diff(ccw$US, lag = 1, differences = 1)


k=7
MAV <- rollmean(first.der,k)

fit8 <- lm(first.der ~ poly(day, 8, raw=TRUE), ccw[-1, ]) # OCTIC!

par(mar=c(5,5,3,2))
with(ccw, plot(day, first.der, 
      main="Daily COVID-19 Cases in the US", cex.main=2,
      axes=FALSE,
      xlab='', 
      ylab='',
      las = 2,
      col=rgb(0.4,0.4,0.8,0.6), pch = 16, cex = .6))

abline(h=0)
abline(h=ccw$first.der[length(ccw$day)], col='red', lty=2)

tck <- seq(min(ccw$day), max(ccw$day), by=10)
axis(1, tck, labels=FALSE)

at2 <- seq(min(first.der),max(first.der)+150000, 0.10 * max(first.der))
axis(side=2, at2, 
     las=2, cex.axis=1, labels = formatC(at2, big.mark = ",", format = "d"))

mtext(strftime(ccw$date[tck], "%b %d"), 1, 1, at=tck, las=2)

lines(fit8$fitted.values, col=alpha('blue',.8), lwd=2)
points(ccw$day, ccw$first.der, main="US covid-19", pch=16, col=rgb(0.8,0.2,0.1,0.6))
points(tail(ccw$day,1), last(ccw$first.der), main="US covid-19", pch=19, cex=1.2, col='red')


ggplot(ccw, aes(x = day, y = first.der)) + geom_point() +
  geom_vline(xintercept = 0, color = "red")  + 
  geom_point(mapping = aes(x = ccw$day, y = ccw$first.der), color = "blue") +
  geom_smooth(method = "lm", formula = y ~ poly(x, 8))
question from:https://stackoverflow.com/questions/65894787/how-to-tailor-the-x-axis-of-a-ggplot-to-include-dates

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

On x-axis keep Date column so that you can use scale_x_date to include the labels and breaks as per your choice.

library(ggplot2)

ggplot(ccw, aes(x = date, y = first.der)) + 
  geom_point() +
  geom_vline(xintercept = 0, color = "red")  + 
  geom_point(color = "blue") +
  geom_smooth(method = "lm", formula = y ~ poly(x, 8)) + 
  scale_x_date(date_labels = '%b %d', breaks = '2 week') + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) + 
  scale_y_continuous(labels = scales::comma)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...