颜色在aes()内部不起作用,并且图例在aes()之外消失

时间:2017-06-12 14:37:55

标签: r ggplot2

我有以下数据集:

Z_CONTRY LOF Z_UCS
Bulgaria  1   16
Greece    3   20
Austria   2   5
Germany   1   28
Ireland   5   68

我在Z_CONTRY列中有大约30个国家/地区。我想要做的是在x.axis上使用LOF,在y.axis上使用Z_UCS,并根据具有“自定义”颜色调色板的国家/地区对散点图上的那些点进行着色,因为我希望它们可以区分(因素) (Z_CONTRY)没有提供好的颜色。)

所以这就是我的所作所为:

 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)

当代码如上所示,我收到一个没有图例的情节:

no legend

当我把尺寸和颜色(或只是颜色)放在aes中时,我选择的颜色不起作用,但我得到了一个传奇: 代码:

  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

如何才能让两种颜色都正常工作并显示图例?

0 个答案:

没有答案