[R] Error in colouring by group in core plot R

Jim Lemon
2020-04-24T10:44:12Z

Hi Luigi,
This is pretty easy using "plot" and "lines":

# assume your example data
plot(g1,col ="red",type = "l", lwd = 2,
     xlab = "Days since start", ylab = "Count of
infections",
     main = "Daily cases of COVID-19")
lines(g2,col="black", lwd = 2)
lines(g3,col="blue", lwd = 2)
text(c(15,80,53),rep(6000,3),
 c("China","Germany","Italy"),col=c("red","black","blue"))

Jim

On Fri, Apr 24, 2020 at 5:25 PM Luigi Marongiu <marongiu.luigi at
gmail.com> wrote:
>
> Hello,
> I am trying to make an epidemic plot of the COVID pandemic using the core
> plot function. I am looking at three countries and the countries are as
> factors. The idea is to colour the entry by country, following this scheme:
> ```
> df = data.frame(index = 1:10,
>                 value = c(rnorm(10), rnorm(10), rnorm(10)),
>                 set = c(rep("Group 1", 3), rep("Group
2", 3), rep("Group
> 3", 4)))
> plot(df$index ~ df$value, col=c("blue", "orange",
"purple")[df$set])
> ```
> But in the actual graph there is only one colour:
> ```
> Index = c(1:101)
> g1 = c(0,259,457,688,769,1771,1459,1737,1981,2099,2589,2825,3235,3884,3694,
>
>  3143,3385,2652,2973,2467,2015,14108,5090,2641,2008,2048,1888,1749,391,
>
889,823,648,214,508,406,433,327,427,573,202,125,119,139,143,99,44,40,
>
>  0,0,0,0,0,0,0,0,0,0,0,0,46,39,78,47,67,55,54,45,0,79,36,35,0,0,0,39,0,
>        0,63,42,46,99,108,89,46,46,0,325,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
> g2 >
c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
>        0,0,0,0,0,0,0,59,283,125,130,240,184,341,401,779,930,924,1214,1459,
>        2095,2960,2993,4528,2516,2509,4183,3935,4332,6615,6933,6824,4740,
>        4450,4923,6173,6813,6365,4933,4031,3252,4288,5633,4939,3936,3281,
>        2402,2218,2138,2543,2945,3699,2327,2018,1323,1388,2195,2481,0,0,
>        0,0,0,0,0,0)
> g3= c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,58,
>       78,72,94,147,185,234,239,573,335,466,587,769,778,1247,1492,1797,977,
>       2313,2651,2547,3497,3590,3233,3526,4207,5322,5986,6557,5560,4789,
>       5249,5210,6203,5909,5974,5217,4050,4053,4782,4668,4585,4805,4316,
>       3599,3039,3836,4204,3951,4694,4092,3153,3961,2667,3786,3493,3491,
>       3047,2256,2729,3370,2646,0,0,0,0,0,0,0,0)
> Incidence = c(g1, g2, g3)
> Country = c(rep("China", length(Index)), rep("Germany",
length(Index)),
>             rep("Italy", length(Index)))
> df = data.frame(Index, Incidence, Country)
> plot(df$Incidence ~ df$Index,
>      col = c("red", "black",
"blue")[df$Country],
>      type = "l", lwd = 2,
>      xaxt = "n",
>      xlab = expression(bold("Date")),
>      ylab = expression(bold("Incidence")),
>      main = "Raw values")
> ```
>
> What am I missing?
> Thank you
>
> --
> Best regards,
> Luigi
>
>         [[alternative HTML version deleted]]