Anna Le Souef
Anna Le Souef

Reputation: 1

Changing the appearance of a line graph

I have plotted a line graph showing monthly numbers of birds n admitted to a veterinary clinic and how this has changed according to year. However I want the colours of each year to appear more distinctive and the x-axis to start on January.

Attempt

library(ggplot2)
df2 |>
  ggplot(aes(x=month, y=n, group=year, colour=year)) +
  geom_line() +
  ggtitle("Monthly admission numbers by year") +
  theme_minimal()+
  scale_color_viridis()

Data

df2 = 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))

Upvotes: -1

Views: 85

Answers (2)

I_O
I_O

Reputation: 6921

In addition to the solution given for the requested layout, you might also want to try alternative layouts to aid the visual appraisal of your data, e.g. to reveal how monthly admission counts changed over the years:

library(ggridges)

df2 <- ungroup(df2)

yearly_totals <- df2 |> summarise(n = sum(n), .by = year)

df2 |>
  mutate(month = factor(month, levels = month.name[12:1])) |> 
  ggplot(aes(year, month, height = n)) +
  geom_ridgeline(scale = .01, color = NA) +
  scale_x_continuous(labels = ~ sprintf('%.0f\n(%.0f)', .x, yearly_totals$n)) +
  theme_light() +
  labs(x = '', y = '')

This information can be extracted from your present arrangement, but we are much more used to reading trends from positions (timeline) than from colours.

ridgeplot

Upvotes: 2

dthorbur
dthorbur

Reputation: 1099

You can use the library RColorBrewer, which has a lot of colour palettes.

For example:

library(ggplot2)
library(RColorBrewer)
df2 |>
  ggplot(aes(x = factor(month, levels = month.name),
      y = n, group = as.factor(year), colour = as.factor(year))) +
  geom_line(size = 1) +
  ggtitle("Monthly admission numbers by year") +
  theme_minimal() +
  labs(y = "Monthly admissions", x = "Month", colour = "Year") +
  scale_colour_brewer(palette="Set2")

Output: img

Use RColorBrewer::display.brewer.all() to display all options.

Edit: Added month ordering part. And to reflect a cleaner solution from a comment.

Upvotes: 1

Related Questions