简体   繁体   English

多行 plot 与 ggplot,geom_line()

[英]multiple lines plot with ggplot, geom_line()

I want to plot multiple lines in the same chart.我想在同一个图表中 plot 多行。 Each line represent an age group (grupo_edad).每条线代表一个年龄组 (grupo_edad)。 In the x axis is the week when the cases are notified (epiweek) and the y axis is the incidence rate (inc). x 轴是通知病例的周(epiweek),y 轴是发病率(inc)。

These is my data:这是我的数据:

inc_015



When I use plotly the result is perfect:当我使用 plotly 时,结果是完美的:

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 时,结果完全不同,这些点是垂直连接的

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?有人能告诉我如何在 ggplot2 中显示我用 plotly 制作的相同图表吗?

For ggplot2 you need to specify the group aesthetic in this case it is the same as colour对于ggplot2您需要指定group美学,在这种情况下它与colour相同

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

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感谢@Sinh Nguyen,很抱歉不知道如何 label 你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM