1
votes

I want to plot multiple lines in the same chart. Each line represent an age group (grupo_edad). In the x axis is the week when the cases are notified (epiweek) and the y axis is the incidence rate (inc).

These is my data:

inc_015



When I use plotly the result is perfect:

p <- plot_ly()%>%
  layout(title = "Curvas de tasas de incidencia de casos notificados de COVID-19 \n en la Región Metropolitana, según grupo etario y semana epidemiológica primeros síntomas",
         xaxis = list(title = "Semana Epidemiológica"),
         yaxis = list (title = "Tasa x 100.000 habitantes") ) %>% 
  add_trace(x = inc_015$epiweek, y = inc_015$inc, type = 'scatter', mode = 'line', name = '0 a 15', 
            line=list(color='#16e3ff',dash='dashed')) %>% 
  add_trace(x = inc_1525$epiweek, y = inc_1525$inc, type = 'scatter', mode = 'line', name = '15 a 25',
            line=list(color='#00c9e4',dash='dashed'))
p

But when I try with ggplot the result is totally different, the points are connected vertically

ggplot(data=incidencia1,
       aes(x=epiweek, y=inc, colour=grupo_edad)) +
  geom_point()

Can someone tell me how to display the same graph that I made with plotly in ggplot2?

2
Hi. Firstly, geom_point() shouldn't be connecting points at all. geom_line() connects them in order of X. geom_path() connects them in order of appearance in the dataframe. Besides that, your ggplot2 code looks correct and should work as intended.cymon
Secondly, your plotly example is not equivalent to the ggplot. Instead of colouring by a variable you are explicitly drawing two separate lines. You can always split your dataframe and draw two separate lines like you did for plotly.cymon
Finally, it is difficult for people to recreate your example when you paste large tables. The output of dput() would have been very useful. Or in this case, the data looks like public covid data, so a link to the source would be the simplest option.cymon

2 Answers

0
votes

For ggplot2 you need to specify the group aesthetic in this case it is the same as colour

ggplot(data=incidencia1,
       aes(x=epiweek, y=inc, group=grupo_edad, colour=grupo_edad)) +
  geom_point() + geom_line()
0
votes

If there is someone that need the answer, here is the solution:

incidencia1$grupo_edad <- incidencia1$grupo_edad %>% as.factor()

f <- ggplot(data=incidencia1,
        aes(x=epiweek, y=inc, group=grupo_edad, col=grupo_edad)) +
        geom_path() + 
        scale_color_manual(name = 'Grupo Etario', labels = c("0-14", "15-24", '25-49', '50-64', '65-80', '>=80'), 
                                      values = c("#FDD700", 'red', "#FE882D", "darkolivegreen3", 'darkblue', 'deepskyblue3')) +
        theme_ipsum_rc(grid = 'Y') + theme(axis.text.x = element_text(angle = 90)) +
        xlab('Semana Epidemiológica') +
        ylab('Tasa de Incidencia') + theme(axis.text.x = element_text(angle = 90)) +
        labs(title = 'Curvas de tasas de incidencia de casos notificados de COVID-19', 
             subtitle = 'Región Metropolitana, según grupo etario y semana epidemiológica primeros síntomas')

f

thanks to @Sinh Nguyen and sorry for don't knnow how to label you