我在让我的折线图看起来更好、更容易阅读时遇到了一些麻烦。
我绘制了一个折线图,显示每月进入兽医诊所的鸟类数量以及该数量随年份的变化。不过我希望每年的颜色显得更加独特,而且x轴从四月开始,我希望它从一月开始。
提前致谢!
df2 %>%
ggplot( aes(x=month, y=n, group=year, colour=year)) +
geom_line() +
ggtitle("Monthly admission numbers by year") +
theme_minimal()+
scale_color_viridis()
数据集在这里:
structure(list(year = c(2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L,
2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L,
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L,
2020L, 2020L, 2020L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2022L, 2022L, 2022L,
2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L,
2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L,
2023L, 2023L, 2023L), month = c("January", "February", "March",
"April", "May", "June", "July", "August", "September", "October",
"November", "December", "January", "February", "March", "April",
"May", "June", "July", "August", "September", "October", "November",
"December", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December", "January",
"February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December", "January", "February",
"March", "April", "May", "June", "July", "August", "September",
"October", "November", "December"), n = c(16L, 20L, 31L, 17L,
18L, 16L, 15L, 32L, 23L, 26L, 7L, 16L, 15L, 20L, 31L, 24L, 16L,
13L, 9L, 13L, 15L, 14L, 15L, 16L, 16L, 27L, 17L, 10L, 12L, 15L,
7L, 17L, 16L, 16L, 14L, 11L, 37L, 39L, 24L, 22L, 17L, 17L, 16L,
38L, 21L, 17L, 11L, 27L, 20L, 42L, 12L, 15L, 12L, 7L, 19L, 25L,
35L, 25L, 9L, 9L, 19L, 53L, 30L, 20L, 22L, 13L, 21L, 44L, 44L,
28L, 17L, 15L)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -72L), groups = structure(list(year = c(2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L,
2019L, 2019L, 2019L, 2019L, 2019L, 2020L, 2020L, 2020L, 2020L,
2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2020L, 2021L,
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L,
2021L, 2021L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L,
2022L, 2022L, 2022L, 2022L, 2022L, 2023L, 2023L, 2023L, 2023L,
2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L, 2023L), month = c("April",
"August", "December", "February", "January", "July", "June",
"March", "May", "November", "October", "September", "April",
"August", "December", "February", "January", "July", "June",
"March", "May", "November", "October", "September", "April",
"August", "December", "February", "January", "July", "June",
"March", "May", "November", "October", "September", "April",
"August", "December", "February", "January", "July", "June",
"March", "May", "November", "October", "September", "April",
"August", "December", "February", "January", "July", "June",
"March", "May", "November", "October", "September", "April",
"August", "December", "February", "January", "July", "June",
"March", "May", "November", "October", "September"), .rows = structure(list(
4L, 8L, 12L, 2L, 1L, 7L, 6L, 3L, 5L, 11L, 10L, 9L, 16L, 20L,
24L, 14L, 13L, 19L, 18L, 15L, 17L, 23L, 22L, 21L, 28L, 32L,
36L, 26L, 25L, 31L, 30L, 27L, 29L, 35L, 34L, 33L, 40L, 44L,
48L, 38L, 37L, 43L, 42L, 39L, 41L, 47L, 46L, 45L, 52L, 56L,
60L, 50L, 49L, 55L, 54L, 51L, 53L, 59L, 58L, 57L, 64L, 68L,
72L, 62L, 61L, 67L, 66L, 63L, 65L, 71L, 70L, 69L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -72L), .drop = TRUE))
您可以使用库
RColorBrewer
,其中有很多调色板。
例如:
library(RColorBrewer)
df2 %>%
ggplot( aes(x=month, y=n, group=as.factor(year), colour=as.factor(year))) +
geom_line(size = 1) +
ggtitle("Monthly admission numbers by year") +
theme_minimal()+
scale_colour_brewer(palette="Set2")
输出:
使用
display.brewer.all()
显示所有选项。