0

I have the following plot in ggplot2 (made-up modification of the ChickWeight dataframe):

require(ggplot2) 
ChickWt <- data.frame(ChickWeight, Sex = rep(c("M", "F"), times = 289))  
p1 <- ggplot(ChickWt, aes(x=Time, y=weight,
colour=Diet, Group = Chick, linetype = Sex)) + geom_line()
p1

which yields the following:

enter image description here

This is fine, except for the legend.

I would like the legend to be of four pairs:

Diet 1-M (colored Purple, solid line), Diet 1-F (colored Purple, brokem line),

Diet 2-M (colored Green, solid line), Diet 2-F (colored Green, brokem line), ....

and so on. Let us say I would like to put the labels in a 2x2 block. I believe that I can put the labels in a 2x2 block using

p1 + guides(fill=guide_legend(nrow = 2, ncol = 2))

but the question I am stumped on is how to make the legend for the four pairs of diet-gender combinations. Any suggestions would be a great help!

user3236841
  • 1,088
  • 1
  • 15
  • 39

1 Answers1

1

You can get a single legend by using the interaction function to generate all the combinations of Diet and Sex and then manually setting the colors and linetypes to the values you desire for each Diet-Sex combination.

require(ggplot2)
require(dplyr)

# Add Sex column
set.seed(104)
ChickWt = ChickWeight %>% group_by(Chick) %>%
  mutate(Sex = sample(c("M","F"),1))

plot.colors = hcl(seq(15,375,length.out=5)[1:4],100,65)

ggplot(ChickWt, aes(x=Time, y=weight, 
                          colour=interaction(Diet,Sex,sep="-"), 
                          linetype = interaction(Diet,Sex,sep="-"), 
                          group=Chick)) + 
  geom_line() +
  scale_colour_manual(values=plot.colors[rep(1:4, 2)], name="Diet-Sex") +
  scale_linetype_manual(values=rep(1:2, each=4), name="Diet-Sex") +
  guides(colour=guide_legend(ncol=2))

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • But there appears to be no 1-F, 3-M, 4-M. Also, how do I get in four separate pairs, for each diet? Thanks! – user3236841 Nov 15 '15 at 01:53
  • The combinations of Diet and Sex that appear in the graph are only those that appear in the data. 1-F, 3-M, and 4-M don't appear in the data and so are absent from the graph. You can see the combinations that occur in the data by doing, for example, `with(ChickWt, unique(paste(Diet,Sex,sep="-")))`, which gives `"1-M" "2-M" "2-F" "3-F" "4-F"`. – eipi10 Nov 15 '15 at 01:56
  • sorry, edited to have 8 combinations in the example now. I still want it in four pairs of Diets (with linetype representing sex). – user3236841 Nov 15 '15 at 02:49
  • See updated code. I created the `Sex` column so as to keep `Sex` constant for each value of `Chick` and to also ensure that every combination of `Diet` and `Sex` is present in the data. – eipi10 Nov 15 '15 at 07:11
  • Thank you! I am almost there. What if I wanted to have two headings: "Male Diets" and "Female Diets" for the two columns? Also, can I put a box around each of the legends (and put them inside the plot, this i know how to do)? Perhaps, it would be better to switch color and linetype in the above graph -- with color for gender and linetype for Diet for two columns. – user3236841 Nov 15 '15 at 14:39
  • Two separate legend titles and putting separate borders around the male and female legends will require (as far as I know) lower-level grid functions to directly manipulate the legend grobs. Probably best to ask a separate question on that. [This SO answer might help](http://stackoverflow.com/questions/27803710/ggplot2-divide-legend-into-two-columns-each-with-its-own-title/27804153#27804153), though it's not a complete solution. – eipi10 Nov 16 '15 at 03:22
  • Thanks! Btw, how does one draw a box around the legend? – user3236841 Nov 16 '15 at 04:03