R 新手,正在尝试制作一个计数图来总结一本书完成的月份。
这是我的代码:
library(ggplot)
book_finished_plot <- ggplot(data, aes(x = mon)) +
geom_bar(aes('Book finished'), width = 30) +
scale_x_date(date_breaks = "1 month",
labels = date_format("%m"))
theme(axis.text.x = element_text(angle = 90, vjust = .5))
book_finished_plot
这给了我错误代码:
Error: Invalid input: date_trans works with objects of class Date only
library(tidyverse)
df |>
mutate(`Book finished` = as.Date(`Book finished`) |> floor_date("month")) |> # group by month and year
ggplot() +
aes(x = `Book finished`) +
geom_bar(stat = "count", fill = "#f48225") +
# decoration stuff below
labs(title = "Books finished by month", x = "Month", y = "Number of books") +
theme(panel.background = element_rect(fill = "#2d2d2d"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(hjust = 0.5))
df <- structure(list(`Book finished` = c("2023-01-03", "2023-01-10",
"2023-03-04", "2023-03-19", "2023-04-02", "2023-04-01", "2023-04-06",
"2023-04-23", "2023-05-21", "2023-05-18", "2023-05-26", "2023-06-01",
"2023-06-05", "2023-06-07", "2023-06-09")), class = "data.frame", row.names = c(NA,
-15L))