如何在ggplot的x轴上插入日期,如下所示:

多亏了SO的帮助,我设法在R的基本绘图函数中有了一个定制的x轴:

然而,用于生成第二个图的关键两行代码:
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)要适应ggplot似乎并不容易,相反,我被困在一天中的数字。顺便说一句,如果你要生成一个带有解决我的问题的修复的图,并且可以想出一种方法来使置信区间更宽,我将不胜感激。
下面是生成这两个图的完整代码:
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))发布于 2021-01-26 09:49:11
在x轴上,保留Date列,以便您可以使用scale_x_date根据您的选择包括标签和分隔符。
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)

https://stackoverflow.com/questions/65894787
复制相似问题