afkar karim
afkar karim

Reputation: 1

How do I color the graph based on state

I'm using this code

plot(AccTEst1$Year, AccTEst1$light.acd, 
     xlab = "Year", ylab = "Light Accident Case", 
     main = "Graph Year against light accident")

enter image description here

EDITED PART

dput(head(AccTEst1, 20))

structure(list(State = c("JOHOR", "JOHOR", "JOHOR", "JOHOR", 
"KEDAH", "KEDAH", "KEDAH", "KEDAH", "KELANTAN", "KELANTAN", "KELANTAN", 
"KELANTAN", "KUALA LUMPUR", "KUALA LUMPUR", "KUALA LUMPUR", "KUALA LUMPUR", 
"MELAKA", "MELAKA", "MELAKA", "MELAKA"), Tot.Acc = c(59501, 62316, 
64600, 64473, 19699, 19935, 20228, 20159, 9603, 9968, 9748, 9383, 
58795, 61872, 64527, 63535, 14720, 15195, 16083, 16375), `Death.Acc
` = c(1001, 985, 1032, 946, 506, 529, 487, 495, 343, 347, 340, 321, 230, 
241, 226, 231, 224, 222, 244, 222), `Seriou.acd` = c(366, 378, 
271, 193, 499, 475, 379, 362, 620, 500, 396, 274, 83, 70, 71, 
142, 147, 169, 109, 187), light.acd = c(1007, 1263, 1055, 687, 
1023, 1036, 787, 863, 1437, 1560, 917, 807, 497, 357, 67, 1166, 
382, 406, 202, 131), Accd.Broken = c(57127, 59690, 62242, 62647, 
17671, 17895, 18575, 18439, 7203, 7561, 8095, 7981, 57985, 61204, 
64163, 61996, 13967, 14398, 15528, 15835), Tot.death = c(1073, 
1073, 1128, 1018, 515, 548, 517, 525, 392, 392, 378, 354, 236, 
249, 243, 238, 240, 243, 258, 236), Serious.acc = c(492, 454, 
356, 281, 608, 486, 403, 365, 800, 647, 524, 346, 83, 73, 75, 
158, 195, 203, 145, 226), `Light.InjCEDERA RINGAN` = c(1254, 
1388, 1233, 759, 1329, 1074, 881, 867, 1819, 1839, 1167, 977, 
498, 365, 71, 1184, 509, 474, 260, 196), Year = c(2011, 2012, 
2013, 2014, 2011, 2012, 2013, 2014, 2011, 2012, 2013, 2014, 2011, 
2012, 2013, 2014, 2011, 2012, 2013, 2014)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

Upvotes: 0

Views: 53

Answers (1)

Narimene LOUATI
Narimene LOUATI

Reputation: 143

if I understood your question correctly, you can make this in you code :

color = "State"

plot(AccTEst1$Year, AccTEst1$light.acd, color = "State",
     xlab = "Year", ylab = "Light Accident Case", 
     main = "Graph Year against light accident")

Upvotes: 2

Related Questions