I have the following data set:
Z_CONTRY LOF Z_UCS
Bulgaria 1 16
Greece 3 20
Austria 2 5
Germany 1 28
Ireland 5 68
I have about 30 countries in the Z_CONTRY column. What i want to do is have LOF on the x.axis, Z_UCS on the y.axis and color those points on the scatter plot based on the country with a "custom" palette of colors because i want them to be distinguishable (the factor(Z_CONTRY) does not give good colors).
So here is what i do:
scatter1 <-ggplot(data1, aes( LOF, Z_UCS))+
geom_point(size = data1$LOF,colour = ifelse(data1$Z_CONTRY=="Austria","burlywood4",
ifelse(data1$Z_CONTRY=="Bulgaria","cadetblue",
ifelse(data1$Z_CONTRY=="Cyprus","chartreuse",
ifelse(data1$Z_CONTRY=="Czech Republic","chocolate",
ifelse(data1$Z_CONTRY=="Greece","chocolate4",
ifelse(data1$Z_CONTRY=="Hungary","coral4",
ifelse(data1$Z_CONTRY=="Italy","black",
ifelse(data1$Z_CONTRY=="Moldova","blue",
ifelse(data1$Z_CONTRY=="Poland","cyan2",
ifelse(data1$Z_CONTRY=="Romania","brown",
ifelse(data1$Z_CONTRY=="Slovakia","darkgoldenrod4",
ifelse(data1$Z_CONTRY=="Slovenia","darkgoldenrod1",
ifelse(data1$Z_CONTRY=="Armenia","darkgreen",
ifelse(data1$Z_CONTRY=="Bosnia&Hertzegovina","darkmagenta",
ifelse(data1$Z_CONTRY=="Belarus","darkolivegreen1",
ifelse(data1$Z_CONTRY=="Switzerland","darkorange",
ifelse(data1$Z_CONTRY=="Estonia","darkorange4",
ifelse(data1$Z_CONTRY=="FYROM","firebrick4",
ifelse(data1$Z_CONTRY=="Croatia","firebrick1",
ifelse(data1$Z_CONTRY=="Kosovo","darkorchid1",
ifelse(data1$Z_CONTRY=="Lietuva","darkorchid4",
ifelse(data1$Z_CONTRY=="Latvia","gold",
ifelse(data1$Z_CONTRY=="Montenegro","gold4",
ifelse(data1$Z_CONTRY=="Nigeria","deeppink4",
ifelse(data1$Z_CONTRY=="Northern Ireland","deeppink",
ifelse(data1$Z_CONTRY=="Republic of Ireland","green4",
ifelse(data1$Z_CONTRY=="Russia","indianred4",
ifelse(data1$Z_CONTRY=="Serbia","orange4","yellow3")))))))))))))))))))))))))))))+
labs(list(y = "Unit cases", x = "Local Outlier Factor"))+
ggtitle(bquote(atop(.("Month End Loading Vusialization"),"")))+
scale_colour_discrete(guide = guide_legend(title = NULL))
print(scatter1)
When the code is as above i receive a plot with no legend:
When i put size and color(or just the colors) in the aes, the colors i have chosen don't work but i get a legend: Code:
scatter1 <-ggplot(data1, aes( LOF, Z_UCS))+
geom_point(aes(size = data1$LOF,colour = ifelse(data1$Z_CONTRY=="Austria","burlywood4",
ifelse(data1$Z_CONTRY=="Bulgaria","cadetblue",
ifelse(data1$Z_CONTRY=="Cyprus","chartreuse",
ifelse(data1$Z_CONTRY=="Czech Republic","chocolate",
ifelse(data1$Z_CONTRY=="Greece","chocolate4",
ifelse(data1$Z_CONTRY=="Hungary","coral4",
ifelse(data1$Z_CONTRY=="Italy","black",
ifelse(data1$Z_CONTRY=="Moldova","blue",
ifelse(data1$Z_CONTRY=="Poland","cyan2",
ifelse(data1$Z_CONTRY=="Romania","brown",
ifelse(data1$Z_CONTRY=="Slovakia","darkgoldenrod4",
ifelse(data1$Z_CONTRY=="Slovenia","darkgoldenrod1",
ifelse(data1$Z_CONTRY=="Armenia","darkgreen",
ifelse(data1$Z_CONTRY=="Bosnia&Hertzegovina","darkmagenta",
ifelse(data1$Z_CONTRY=="Belarus","darkolivegreen1",
ifelse(data1$Z_CONTRY=="Switzerland","darkorange",
ifelse(data1$Z_CONTRY=="Estonia","darkorange4",
ifelse(data1$Z_CONTRY=="FYROM","firebrick4",
ifelse(data1$Z_CONTRY=="Croatia","firebrick1",
ifelse(data1$Z_CONTRY=="Kosovo","darkorchid1",
ifelse(data1$Z_CONTRY=="Lietuva","darkorchid4",
ifelse(data1$Z_CONTRY=="Latvia","gold",
ifelse(data1$Z_CONTRY=="Montenegro","gold4",
ifelse(data1$Z_CONTRY=="Nigeria","deeppink4",
ifelse(data1$Z_CONTRY=="Northern Ireland","deeppink",
ifelse(data1$Z_CONTRY=="Republic of Ireland","green4",
ifelse(data1$Z_CONTRY=="Russia","indianred4",
ifelse(data1$Z_CONTRY=="Serbia","orange4","yellow3"))))))))))))))))))))))))))))))+
labs(list(y = "Unit cases", x = "Local Outlier Factor"))+
ggtitle(bquote(atop(.("Month End Loading Vusialization"),"")))
print(scatter1)
How can i get both the colors working and the legend showing up?