Problem with geom_point ggplot

Hi there mates! I´m currently working with a coronavirus dataset and I have some problems with the geom_point function. (The dataframe I am showint you you is a simplification of what i am currently working):
1- My main problem is that I´d like to point only the max value of "y" in the 3 lines. I am trying something like: "+ geom_point(max(y))
2-Secondly I´d line to put kind of abline in each of the lines that connect this point with its x value
3-If its not too much, just with geom_text() put the "y" value in each point automatically.

Here is the code. If anyone can help me with something of that, I´d be so much grateful…

Rubén.

#CODE
ObservationDate <- c("1/3", "2/3", "3/3","4/3","5/3","6/3","7/3","8/3","9/3")
Confirmed_per_day <- c(334,345,367,405,447,480,456,421,400)
Deaths_per_day <- c(123,143,145,180,236,246,280,265,247)
Recovered_per_day <- c(245,247,235,270,306,337,355,367,340)
coronavirus_daily_Spain <- data.frame(ObservationDate,Confirmed_per_day,Deaths_per_day,Recovered_per_day)
View(coronavirus_daily_Spain)
#GRAPH
library(ggplot2)
q <- ggplot(data=coronavirus_daily_Spain, aes(x=ObservationDate))+
geom_line(aes(y = Confirmed_per_day, group=1, color = "steelblue"), size=2) +
geom_line(aes(y = Deaths_per_day, group=1, color = "darkred"), size=2) +
geom_line(aes(y = Recovered_per_day, group=1, color = "green"), size=2) +
scale_color_discrete(name = "Data Series", labels = c("Deaths", "Recovered", "Confirmed"))
q + theme(axis.text.x = element_text(angle = 90, hjust = 1))
print(q + ggtitle("Evolution of Covid-19") + theme(plot.title = element_text(hjust = 0.5)))

maybe you oversimiplified your example too much as I dont understand your requirements.
you say the max value of y in the 3 lines, you cant mean at each x, because that would be y itself as there are no repeated measures shown. So you want to just show horizontal lines ?


and draw ablines up to the horizonal lines at each x ? wont it be an unreadable mess ?
what am i missing...

Sorry maybe I explained myself not so good. This is the plot you got from the code I gave to you and and drew what I am trying to get back... maybe that image can help you.

Thank you so much.

Rubén.

This could work as a starting point for you

library(tidyverse)
library(lubridate)

# Sample data on a copy/paste friendly format
coronavirus_daily_Spain <- data.frame(
    stringsAsFactors = FALSE,
    ObservationDate = c("1/3","2/3","3/3","4/3",
                        "5/3","6/3","7/3","8/3","9/3"),
    Confirmed_per_day = c(334, 345, 367, 405, 447, 480, 456, 421, 400),
    Deaths_per_day = c(123, 143, 145, 180, 236, 246, 280, 265, 247),
    Recovered_per_day = c(245, 247, 235, 270, 306, 337, 355, 367, 340)
)

long_data <- coronavirus_daily_Spain %>% 
    gather(Variable, Value, -ObservationDate) %>%
    mutate(ObservationDate = dmy(paste0(ObservationDate, "/2020")))

max_values <- long_data %>% 
    group_by(Variable) %>% 
    filter(Value == max(Value))

long_data %>% 
    ggplot(aes(x = ObservationDate, y = Value))+
    geom_line(aes(color = Variable), size=2) +
    geom_point(data = max_values) +
    geom_segment(data = max_values,
               aes(xend = ObservationDate, yend = 0),
               linetype = "dashed") +
    geom_text(data = max_values,
              aes(label = Value),
              vjust = -1) +
    labs(title = "Evolution of Covid-19") +
    scale_color_discrete(name = "Data Series",
                         labels = c("Confirmed", "Deaths", "Recovered")) +
    scale_x_date(date_breaks = "1 day", date_labels = "%d/%m") +
    coord_cartesian(clip = "off") +
    theme(axis.text.x = element_text(angle = 90, hjust = 1),
          plot.title = element_text(hjust = 0.5))

Created on 2020-05-03 by the reprex package (v0.3.0)

1 Like

OMG that´s amazing. I hope to get your level same day!
Thank you very much. I hope you have a wonderful day!

Rubén.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.