How to plot in time series with ggplot, geom_line, with line group by 'answer'

Viewed 13

I have plotted this time series data that contains three variables: date, answer, and counts.

I would like to plot in time series where the lines representing categories of the answers. I have tried this codes below, but result are not as expected: I did't get any line. Could anyone help?

     bird %>% ggplot(aes(date, counts)) + 
geom_line(aes(colour = answer), show.legend = FALSE) +
      geom_dl(aes(label = answer), 
        method = "last.points",
        position = position_nudge(x = 0.3)) +
      labs(x = 'year', y = 'percent',
        title = "Attitude towards X",
        subtitle = "Do you agree or disagree?",
        caption = "Data: Data Center") + 
    coord_cartesian(clip = "off") + 
    theme_ipsum(grid = "Y", ticks = TRUE)

enter image description here

THEN, I changed the answer value into numbers, where 'agree' = 1, 'disagree' = 2, and 'not know' = 3, and ran the same code resulting in similar plot,below.

enter image description here

How to plot it with the answer categories represented in 3 different lines on the plot, and its name at the right edge of each line. Many thanks for your help.

So, after a while, I was able to see the line with this updated code:

 p <- ggplot(data=snow,
            aes(x=date, y=counts, group=answer, col=answer)) +
  geom_line() +
  geom_dl(
    aes(label = answer), 
    method = "last.points",
    position = position_nudge(x = 0.5)) +
  labs(
    x = 'year',
    y = 'percent',
    title = "Attitude towards X",
    subtitle = "Do you agree or disagree?",
    caption = "Data: Data Center") +
  coord_cartesian(clip = "off") +
  theme_ipsum(grid = "Y", ticks = TRUE)

enter image description here

But, I need to ADD the name to the three LINES and also the specific value (percentage) to the line based on the date points on the X axis.

0 Answers
Related