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

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Fri Apr 24 13:31:14 CEST 2020


Hello,

This is probably off-topic since you have chosen base graphics but this 
is much easier with ggplot.


library(ggplot2)

ggplot(df, aes(Index, Incidence, colour = Country)) +
   geom_line() +
   scale_colour_manual(values = c("red", "black", "blue")) +
   theme_minimal()


Hope this helps,

Rui Barradas


Às 08:24 de 24/04/20, Luigi Marongiu escreveu:
> 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
>



More information about the R-help mailing list