2

While different colors are shown in the plot, the legend shows only one color. My objective is to show the legend as is. Also, I am NOT looking to use shapes between 21 and 25. I want to use a different shape e.g. Shape = 2.

Here's the source code:

#catsM dataset is in MASS package that is shipped by CRAN
     testM<-catsM
     testM[1:10,"Sex"] <- "F"
     ggplot(testM,aes(Bwt,Hwt)) +
      geom_point(aes(color=Sex), shape = 2) + 
        geom_smooth(method = "glm", se = FALSE, 
                    aes(linetype = "glm line"), color = "red") + 
        geom_smooth(method = "lm", span = 0.5, 
                    aes(linetype = "lm line"), color = "yellow") + 
        scale_color_manual(values = c("magenta","black"))

Here's the output. We can see that the color code of the second line lm overrides the red color of glm. Is there any fix for this? I'd appreciate your thoughts. enter image description here


I am posting dput():

dput(catsM)
structure(list(Sex = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("F", "M"), class = "factor"), 
    Bwt = c(2, 2, 2.1, 2.2, 2.2, 2.2, 2.2, 2.2, 2.2, 2.2, 2.2, 
    2.3, 2.4, 2.4, 2.4, 2.4, 2.4, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 
    2.5, 2.5, 2.6, 2.6, 2.6, 2.6, 2.6, 2.6, 2.7, 2.7, 2.7, 2.7, 
    2.7, 2.7, 2.7, 2.7, 2.7, 2.8, 2.8, 2.8, 2.8, 2.8, 2.8, 2.8, 
    2.9, 2.9, 2.9, 2.9, 2.9, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3.1, 
    3.1, 3.1, 3.1, 3.1, 3.1, 3.2, 3.2, 3.2, 3.2, 3.2, 3.2, 3.3, 
    3.3, 3.3, 3.3, 3.3, 3.4, 3.4, 3.4, 3.4, 3.4, 3.5, 3.5, 3.5, 
    3.5, 3.5, 3.6, 3.6, 3.6, 3.6, 3.7, 3.8, 3.8, 3.9, 3.9), Hwt = c(6.5, 
    6.5, 10.1, 7.2, 7.6, 7.9, 8.5, 9.1, 9.6, 9.6, 10.7, 9.6, 
    7.3, 7.9, 7.9, 9.1, 9.3, 7.9, 8.6, 8.8, 8.8, 9.3, 11, 12.7, 
    12.7, 7.7, 8.3, 9.4, 9.4, 10.5, 11.5, 8, 9, 9.6, 9.6, 9.8, 
    10.4, 11.1, 12, 12.5, 9.1, 10, 10.2, 11.4, 12, 13.3, 13.5, 
    9.4, 10.1, 10.6, 11.3, 11.8, 10, 10.4, 10.6, 11.6, 12.2, 
    12.4, 12.7, 13.3, 13.8, 9.9, 11.5, 12.1, 12.5, 13, 14.3, 
    11.6, 11.9, 12.3, 13, 13.5, 13.6, 11.5, 12, 14.1, 14.9, 15.4, 
    11.2, 12.2, 12.4, 12.8, 14.4, 11.7, 12.9, 15.6, 15.7, 17.2, 
    11.8, 13.3, 14.8, 15, 11, 14.8, 16.8, 14.4, 20.5)), .Names = c("Sex", 
"Bwt", "Hwt"), row.names = c("1", "2", "3", "4", "5", "6", "7", 
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", 
"41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", 
"52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", 
"63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", 
"74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", 
"85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", 
"96", "97"), class = "data.frame")
watchtower
  • 4,140
  • 14
  • 50
  • 92
  • I get a warning message when I try `data('catsM', package="MASS")` – IRTFM Aug 27 '16 at 07:48
  • @42- Sorry about the warning issue. I am not sure why I am not getting it. So, I have added dput(). – watchtower Aug 27 '16 at 07:55
  • @42- try `data('cats', package = 'MASS')`. catsM didn't worked for me either, but MASS documentation now points toward 'cats' rather than 'catsM'. – Pj_ Aug 27 '16 at 09:37
  • If you want colour in a legend, you have to include color inside `aes`thetics. But then you will have two color mappings: linetype and sex. `ggplot` does not allow this. To get round the problem, use a hollow point for the sex legend, and use `fill=Sex` in place of `color=Sex`. See the answer to your [earlier question](http://stackoverflow.com/questions/39171511/displaying-proper-colors-in-legend-for-multiple-layers). – Sandy Muspratt Aug 27 '16 at 09:44
  • Sandy Muspratt I had no intentions to double-post. I think I have mentioned that I want the shape to be not equal 21. Unfortunately, this doesn't help me.....This is again quick-fix and not a programmatic solution for non-fillable shapes (i.e. shapes outside the range 21 and 25), something I was looking for....I hope you understand... – watchtower Aug 27 '16 at 10:52

2 Answers2

3

I still haven't fully grasped all the quirks of ggplot2's legends, but if you want to force it, the guides options will allow you to manually set it:

testM<-catsM
testM[1:10,"Sex"] <- "F"
ggplot(testM,aes(Bwt,Hwt)) +
  geom_point(aes(color=Sex), shape = 2) + 
  geom_smooth(method = "glm", se = FALSE, 
          aes(linetype = "glm line"), color = "red") + 
  geom_smooth(method = "lm", span = 0.5, 
          aes(linetype = "lm line"), color = "yellow") + 
  scale_color_manual(values = c("magenta","black")) +
  guides(linetype = guide_legend(override.aes = list(color = c("red", "yellow"))))
gambrel
  • 56
  • 2
0

For discrete variables mapping aesthetics to shape is another option. Not sure of how good it'll be when it comes to design aesthetics and usa:

  ggplot(testM,aes(Bwt,Hwt)) +
  geom_point(aes(shape= Sex)) + 
  geom_smooth(method = "glm", se = FALSE, 
              aes(linetype = "glm", color  = 'glm')) + 
  geom_smooth(method = "lm", span = 0.5, 
              aes(linetype = "lm", color ='lm'), size = 4) + 

  scale_linetype_discrete(name = "Method") +   
  scale_color_manual(name = "Method", values = c('red', 'yellow'))
Pj_
  • 824
  • 6
  • 15
  • Pj_ I think I have mentioned that I want the shape to be not equal 21. Unfortunately, this doesn't help me.....This is again quick-fix and not a programmatic solution for non-fillable shapes (i.e. shapes outside the range 21 and 25), something I was looking for... – watchtower Aug 27 '16 at 10:48
  • @ss0208535 Please, check and update your question and add this information that you're not really looking to use shapes inside 21 & 25. It might mislead a lot of users who are trying to help you. – Pj_ Aug 27 '16 at 11:12