How to insert dates in the x axis of a ggplot as this:
Thanks to the help from SO, I managed to have a tailored x-axis in the basic plotting function in R:
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