Search code examples
rggplot2lda

Problems with to legends in a lda plot using ggplot2


I am having trouble with a legend in a lda analysis. Here is toy data:

>d_e_a.train
   Lymphoprol. CORT Testo  FDL RBC.Ab. ifn.g il.4 Profile
52        0.00 0.58  1.94 2.54       6    98   40     Med
81       22.23 0.58  0.05 1.56       4   203   45     Med
66        5.31 1.75  0.30 2.73       3    49   74    High
62       35.00 0.81  0.95 4.30       6   322   60     Low
9         6.52 2.36  0.03 0.92       4    51   75    High
70       13.27 0.47  1.67 2.57       3   278   75     Med
56       18.23 0.46  1.89 2.99       4    54   60    High
72       31.25 0.31  1.52 3.37       5   305   57     Low
90       22.09 0.40  0.06 1.62       5   254   58     Med
37        4.32 1.34  0.05 0.71       3    41   73    High
3        15.65 0.50  0.07 0.97       5   280   67     Med
17       39.32 1.71  0.30 2.06       2    93   53    High
57       19.25 1.15  0.05 1.75       5    95   73     Med
24       17.03 0.14  1.28 3.22       4    79   77     Med
85       13.73 0.52  1.59 2.20       3    62   75     Med
41       23.16 0.89  0.09 1.48       2    99   57     Med
65       29.25 0.28  0.04 2.56       5   298   55     Low
75        0.00 0.86  0.11 1.65       3   110   47     Med
22       14.25 1.09  1.46 1.96       5    76   69     Med
20       35.14 0.26  1.12 5.16       6   282   47     Low
83       36.94 0.55  1.62 2.15       4   298   60     Low
45       28.58 1.50  0.21 1.41       5   201   65     Med
2        13.91 0.65  1.34 2.27       6   195   58     Med
73        0.00 0.99  0.09 0.92       3   133   77     Med
29       35.80 0.12  0.01 1.80       7   307   65     Low`

and this is the model: model_a <- lda(Profile ~., data = d_e_a.train)

when I try to plot it using the following code I get two legends as it can be seen in the plot

library(ggplot2)
library(ggfortify)
library(devtools)
install_github('fawda123/ggord')
library(ggord)
plota<-ggord(model_a, d_e_a.train$Profile)+
  theme_classic()+ 
  scale_fill_manual(name = "Profile", 
                  labels = c("Fischer - like", "Lewis - like", "Medium"))+
  theme(text = element_text(size = 20 ),
        axis.line.x = element_line(color="black", size = 1),
        axis.line.y = element_line(color="black", size = 1),
        axis.text.x=element_text(colour="black",angle = 360,vjust = 0.6),
        axis.text.y=element_text(colour="black"))
plota

I would like to get only the legend that is seen in the top. Regards example of plot with two legends


Solution

  • You need to have both a fill scale and a color scale with the same labels. You also need to remove the shape guide that this function seems to add, even though the shape of the points appears constant.

    ggord(model_a, d_e_a.train$Profile)+
      theme_classic()+ 
      scale_fill_discrete(name = "Profile", 
                        labels = c("Fischer - like", "Lewis - like", "Medium"))+
      scale_color_discrete(name = "Profile",
                           labels = c("Fischer - like", "Lewis - like", "Medium"))+
      theme(text = element_text(size = 20 ),
            axis.line.x = element_line(color="black", size = 1),
            axis.line.y = element_line(color="black", size = 1),
            axis.text.x=element_text(colour="black",angle = 360,vjust = 0.6),
            axis.text.y=element_text(colour="black")) +
      guides(shape = guide_none())
    

    enter image description here