I would like to edit my chart by displaying a single maximum value

Asked

Viewed 73 times

2

Hello guys I’m having trouble displaying a value on my chart, it’s a graph of the evolution of the number of deaths in my city, my X axis is composed of March dates so far, and the Y axis with the number of Deaths,when I used the geom_text function the program plots all values at all their points. My goal is to display a single value, the maximum value. I’ve tried it in several ways but it always gives an error. I’ll try to show you here more clearly.

My Spreadsheet in excel is as follows:

inserir a descrição da imagem aqui

Below the code I used with geom_text

ObitosSet<- rep(c('date','numeric'),times=c(1,1))
Obitos.Set<- read_excel('OBITOS - 01-09-2020.xlsx', sheet = 2,
                           col_types = ObitosSet)

ggplot(Obitos.Set,aes(Data,Q.Obitos))+
  geom_line(col= "orange", size= 0.7)+
  theme_bw()+
  geom_text(aes(label= Q.Obitos),vjust=-0.5,size=3.5)+
  scale_y_continuous(limits= c(0,12),breaks = seq(from= 0, to= 20, by= 1))+
  labs(x= "",y= "Óbitos",
       fill= NULL,
       title = "Evolução dos casos de Óbitos por mês na cidade de Campina Grande-PB",
       caption = "FONTE: Secretaria Municipal de Saúde, Diretoria de Vigilancia em Saúde - DVS, Setembro de 2020")+
  geom_point(col= "red")

Isso foi o resultado


Only I want to plot a single value, the maximum value 9, hence using the following code:

ObitosSet<- rep(c('date','numeric'),times=c(1,1))
Obitos.Set<- read_excel('OBITOS - 01-09-2020.xlsx', sheet = 2,
                           col_types = ObitosSet)

p.data<- data.frame(Data=c("2020-06-21"))
p.data$Data<- ymd(p.data$Data)
ValorY<- data.frame(Resp=c("9 (Máximo)"),MaxOb=c(9.5))

ggplot(Obitos.Set,aes(Data,Q.Obitos))+
  annotate("text",x= p.data$Data[2], y= ValorY$MaxOb, label= ValorY$Resp)+
  geom_line(col= "orange", size= 0.7)+
  theme_bw()+
  scale_y_continuous(limits= c(0,12),breaks = seq(from= 0, to= 20, by= 1))+
  labs(x= "",y= "Óbitos",
       fill= NULL,
       title = "Evolução dos casos de Óbitos por mês na cidade de Campina Grande-PB",
       caption = "FONTE: Secretaria Municipal de Saúde, Diretoria de Vigilancia em Saúde - DVS, Setembro de 2020")+
  geom_point(col= "red")

There goes the following mistake:"Error: Invalid input: date_trans Works with Objects of class Date only"

Now interesting is that the function is correct, because when I don’t specify the type of graph it plots, then when I add the type of graph gives this error.

Personal I am new in this area and I have tried in several ways to do this, I went around and always fell into this error, or a Posixct type error(When I put the annotate function after the geom_line function).

I hope I was clear.

  • Before I tried the following command: annotate("text",x= as.Date('2020-06-21'), y= 10, label= "9 (Maximum)"), after converting to date with lubrication, but also gave me the same errors mentioned above.

1 answer

0


I do not know if I have interpreted the question correctly, but it follows a possible solution:

Complete code:

library(readxl)
library(dplyr)
library(lubridate)

df <- read_excel('./dataset-obitos.xlsx')
df$Data <- as.POSIXct(df$Data, format="%d/%m/%Y")

max_ob <- df %>% group_by(mes = floor_date(Data, 'month'))%>%
  summarise(maximo = max(Q))
        
library(ggplot2)

ggplot(data = df, aes(x = Data,y = Q)) +
  geom_line(stat = "identity") + 
  geom_text(aes(label = Q), 
            position = position_dodge(width=0.9), 
            vjust = -0.25) 

ggplot(data = max_ob, aes(x = mes, y = maximo))  +
  geom_line(stat = "identity") +
  geom_text(aes(label = maximo), 
            position = position_dodge(width=0.9), 
            vjust = -0.25)

Here we group by month and summarize with the highest value found:

max_ob <- df %>% group_by(mes = floor_date(Data, 'month'))%>%
  summarise(maximo = max(Q))

Output from the first chart: Gráfico 1 This shows all separate deaths.


Output from the second chart: Gráfico 2 This shows only the largest deaths within the month.


Third graph: Gráfico 3

This shows only the highest number of general deaths.

Here we use annotate:

library(readxl)
library(lubridate)

df <- read_excel('./dataset-obitos.xlsx')
df$Data <- as.POSIXct(df$Data, format="%d/%m/%Y")
    
library(ggplot2)

ggplot(data = df, aes(x = Data,y = Q)) +
  annotate('text', 
           x = df$Data[which.max(df$Q)],
           y = df$Q[which.max(df$Q)], 
           label = df$Q[which.max(df$Q)], color = 'red')+
  geom_line(stat = "identity")
  • Jeez mass, It gets show also, in fact I would like the graph to remain the same and instead of showing all the values, show only the value of the peak, which was on 06/21/2020 where had 9 deaths. But this also gets show, vlw by tip.

  • I added a new example only with the highest number of deaths.

  • Wow! That’s right buddy! Thanks for the tip.

  • If the answer is what you were looking for, consider marking as accepted. This helps people visualize that the question has already been solved and also encourages others to answer their questions. See how Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.