r - 当 x-axis values 为 dates 时更改刻度间隔

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

我已将我的 date 变量转换为类变量,以便能够更改 x-axis 上的 date 间隔。当我使用“scale_x_date”添加最后一行代码时,会出现此错误消息:“错误:无效输入:date_trans 仅适用于 Date 类的对象”。

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

这是我到目前为止使用的代码:

# 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

回答1

# 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")```

相似文章

go - golang 如何从字符串中查找表情符号?

我想查找表情符号是否存在并替换为字符串(HTMLunicode)。(符文到字符串)例如,这是句子“我喜欢你哈哈哈😀你好。”这就是结果。“我喜欢你哈哈哈😀你好。”表情符号和表情符号位置是随机的。我将...