0

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:

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)

with legend

How can i get both the colors working and the legend showing up?

Emil Filipov
  • 19
  • 1
  • 4
  • 1
    See `scale_color_identity`. – aosmith Jun 12 '17 at 14:42
  • There are several pieces of information missing: (1) a good [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610); (2) the code when you put colour inside the `aes` – Jaap Jun 12 '17 at 14:44
  • 1
    It hurts just looking at that nested series of `ifelse` statements. To make your life easier, create a vector of colors this way: `countries = unique(data1$Z_CONTRY); my_colors = c("red", "yellow", "blue", ..., "purple", "orange"); my_colors = setNames(my_colors, countries)`. You can also create the color vector more easily by using one of the color functions. For example, `my_colors = hcl(seq(0,360,length=length(countries) + 1)[1:length(countries)], 100, 65)`. – eipi10 Jun 12 '17 at 14:52
  • 1
    Then, instead of `scale_colour_identity`, inside `aes` do `aes(colour=Z_CONTRY)`, and then use `scale_colour_manual(values=my_colors)`. – eipi10 Jun 12 '17 at 14:56
  • @eipi10 Hi! Thank you for the insight. I never thought of using the setNames function. – Emil Filipov Jun 13 '17 at 06:28

0 Answers0