当 x 轴值为日期时更改刻度间隔

Changing tick intervals when x-axis values are dates

我想将 x 轴上的间隔从每日滴答声更改为每月滴答声,以显示一段时间内的总 COVID cases/million。现在,我的 x 轴上每天都有刻度,因此无法看到 x 轴上发生了什么。[当前图表的图片][1]

我已将日期变量转换为 class 变量,以便能够更改 x 轴上的日期间隔。当我使用“scale_x_date”添加最后一行代码时,会出现此错误消息:“错误:无效输入:date_trans works with objects of class Date only”。

如能提供解决此问题的任何提示,我将不胜感激!

这是我目前使用的代码:

# load our world in data covid data (https://github.com/owid/covid-19-data/tree/master/public/data/)
owid_data <- read.csv("owid-covid-data.csv")

# subset
df <- subset(owid_data, select=c(iso_code, location, date, total_cases_per_million, new_cases_per_million, new_cases_smoothed_per_million, total_deaths_per_million, new_deaths_per_million, new_deaths_smoothed_per_million, reproduction_rate, stringency_index, excess_mortality, excess_mortality_cumulative_per_million))
df <- subset(df, location == "Australia" | location == "Denmark" | location == "Germany"| location == "Netherlands" | location == "New Zealand"| location == "Sweden"| location == "Switzerland"| location == "United Kingdom")


# Convert "date variable" into class Date variable 
class(date)
date <- as.Date(df$date, "%Y-%m-%d")
class(date)

# Ggplot cases/million - visualise Time-Series Data with Line Plots
ggplot(df, aes(x=date, y=total_cases_per_million, group=location))+
  geom_line(aes(col=location), size=0.7)+
  theme_classic() + 
  theme(legend.title = element_text(face="bold"), plot.title = element_text(face="bold"), plot.caption = element_text(hjust = 0))+
  labs(col = "Country", 
       title = "Total confirmed cases of COVID-19 per million people",
       x = "Date",
       y = "COVID-19 cases per million people", 
       caption = "Source: COVID-19 Data Repository by the Center for Systems Science and Engineering (CSSE) at Johns Hopkins University")+
  scale_y_continuous(breaks = seq(0,600000, by=100000), labels = scales::comma)+
  scale_x_date(limits = as.Date(c("2020-01-26","2022-05-16")), date_breaks = "1 month", date_minor_breaks = "1 day", date_labels = "%Y-%m-%d")```


  [1]: https://i.stack.imgur.com/lSHAy.png
# load our world in data covid data (https://github.com/owid/covid-19-data/tree/master/public/data/)
owid_data <- read.csv("owid-covid-data.csv")

# subset
df <- subset(owid_data, select=c(iso_code, location, date, total_cases_per_million, new_cases_per_million, new_cases_smoothed_per_million, total_deaths_per_million, new_deaths_per_million, new_deaths_smoothed_per_million, reproduction_rate, stringency_index, excess_mortality, excess_mortality_cumulative_per_million))
df <- subset(df, location == "Australia" | location == "Denmark" | location == "Germany"| location == "Netherlands" | location == "New Zealand"| location == "Sweden"| location == "Switzerland"| location == "United Kingdom")


# Convert "date variable" into class Date variable 
class(df$date)
df$date <- as.Date(df$date, "%Y-%m-%d")
class(df$date)

# Ggplot 
ggplot(df, aes(x=date, y=total_cases_per_million, group=location))+
  geom_line(aes(col=location), size=0.7)+
  theme_classic() + 
  theme(legend.title = element_text(face="bold"), plot.title = element_text(face="bold"), plot.caption = element_text(hjust = 0))+
  labs(col = "Country", 
       title = "Total confirmed cases of COVID-19 per million people",
       x = "Date",
       y = "COVID-19 cases per million people", 
       caption = "Source: COVID-19 Data Repository by the Center for Systems Science and Engineering (CSSE) at Johns Hopkins University")+
  scale_y_continuous(breaks = seq(0,600000, by=100000), labels = scales::comma)+
  scale_x_date(limits = as.Date(c("2020-01-26","2022-05-16")), date_breaks = "1 month", date_minor_breaks = "1 day", date_labels = "%Y-%m-%d")```