mercredi 1 juillet 2020

Frustrating issue with CORRECTLY assigning color, using ifelse in mycol argument, to highlight line in xyplot spaghetti plot

I am trying to brightly color one line in my spaghetti plot to distinguish it from the others (which other lines are grey). Using a simple ifelse statement in the color argument (or creating a separate variable - tried both) should do the trick, but this is not aligning with the correct identifying country labels. Conditioning on country label does correctly identify specific countries of interest when plotting, or when labeling. However, when using this in an ifelse statement with the col = argument, the highlighted line gets mixed up with the incorrect country label. I'm wondering if anyone has come across this issue before. Thanks very much in advance for any insights. Reprex included below.

Data <- data.frame(
    score = round(runif(25,0,1), 2), 
    label = c("ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL","ZAF", "MEX", "URY", "ROU", "CHL"), 
    year = c(2000,2000,2000,2000,2000,2001, 2001, 2001,2001,2001, 2002, 2002,2002,2002,2002,2003,2003,2003,2003,2003,2004,2004,2004,2004,2004))

library(lattice)
library(directlabels)

## just plotting ZAF label - this is what it should look like
xyplot(Data$score[Data$label == "ZAF"] ~ Data$year[Data$label == "ZAF"], type = "l", xlab = "Year", ylab = "Democracy Score", col = ifelse(Data$label == "ZAF", "red", "grey"))

## now plotting all labels - highlights the wrong label in red, should still highlight ZAF 
myplot <- xyplot(Data$score ~ Data$year, group = Data$label, type = "l", xlab = "Year", ylab = "Democracy Score", col = ifelse(Data$label == "ZAF", "red", "grey"))
direct.label(myplot)```

Aucun commentaire:

Enregistrer un commentaire