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]]
Hi Luigi, the problem is not the first graph vs the second graph. The first graph would also show the same effect if you added type='l' to the plot command. There are various ways to approach this. A quick search turned up the following which gives you different options. https://stackoverflow.com/questions/14860078/plot-multiple-lines-data-series-each-with-unique-color-in-r HTH, Eric On Fri, Apr 24, 2020 at 10:25 AM 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]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
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
>
iuke-tier@ey m@iii@g oii uiow@@edu
2020-Apr-24 12:51 UTC
[R] [External] Re: Error in colouring by group in core plot R
Or using matplot:
matplot(cbind(g1, g2, g3), type = "l",
col = c("red", "black", "blue"), lty = 1,
lwd = 2)
Best,
luke
On Fri, 24 Apr 2020, Jim Lemon wrote:
> 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]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa Phone: 319-335-3386
Department of Statistics and Fax: 319-335-3017
Actuarial Science
241 Schaeffer Hall email: luke-tierney at uiowa.edu
Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu