0

I am using face_wrap to present my results in four different groups (graphs that correspond to the variable "Tipo"). Each group has its own shape, and within each group, I use different colors to represent different types of results (in this case, a different type of illness "Enfermedad"). Thus, I cannot assign "shape" and "color" to the same variable, and also merge both columns doesn't work for my case. So basically, I've managed to assign the four different shapes to each group and the 16 different colors, but what I haven't managed is to create a legend with both color and shape. Thank you in advance!

The table I use looks like this:

 Enfermedad               Casos     Año        Tipo
 Amebiasis intestinal      100      1998       Parasitaria
 Amebiasis intestinal      250      1999       Parasitaria
 Fiebre tifoidea           300      1998       Bacteriana
 Fiebre tifoidea           225      1999       Bacteriana
 Hepatitis vírica A         50      1998       Vírica
 Hepatitis vírica A         33      1999       Vírica

Here is my code:

library(RColorBrewer)

EnfermGastro<- read_excel("...")

colourCount = length(unique(EnfermGastro$Enfermedad))
getPalette = colorRampPalette(brewer.pal(16, "Set1"))

ggplot(EnfermGastro, aes(x=Año,y=Casos,shape=Tipo, colour=Enfermedad)) +

geom_point(size=2.5) +

scale_shape_manual(values =c(16, 1, 18, 8)) +

scale_fill_manual(values= getPalette(colourCount)) +

scale_x_continuous( breaks=c(1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,
                           2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018)) +
scale_y_continuous(labels=comma) +

theme_bw() +
theme(axis.line=element_line(colour = "black"),
      panel.grid.major.y = element_line(colour="grey75", linetype="dotted"),
      panel.grid.major.x = element_line(colour="grey75", linetype="dotted"),
      panel.border=element_blank(),
      legend.background=element_rect(fill="grey80", colour="black"),
      legend.title = element_text(size= 10),
      legend.text = element_text(size= 8),
      legend.position="bottom") +

   facet_wrap(~Tipo, scale= "free")

enter image description here

Emil
  • 23
  • 3
  • check out [this similar question](https://stackoverflow.com/questions/12410908/combine-legends-for-color-and-shape-into-a-single-legend) I think this might answer your question for a legend with both color and shape – Daniel_j_iii May 28 '20 at 02:03
  • Hi Daniel, thanks I already check that question and it does not answer my problem. In the answers to that question, they propose using the same variable for "shape" and "color" which doesn't work for me because for the variable "Enfermedad" I have 16 different types. So to begin with, if I use the solution proposed on the other question I would have to assign 16 different colors in scale_color_manual. – Emil May 28 '20 at 16:00
  • Also, I want to use face_wrap using the variable "Tipo" to produce the 4 different graphs, I use that variable "Tipo" to assign the shape too which is a bit redundant, but what I'm lokking for is that in the legend one can see the colour of the illness and its shape (using only 4 shapes), so the colour allows to see the trend of each illness within the graph while the shape just makes more easy to differentiate among types (i.e. "Tipos) of illness – Emil May 28 '20 at 16:01

1 Answers1

0

Ok, here is a possible solution, not the best one because I only wanted to have 4 different shapes, one type of shape in each facet, but at least I managed to create a legend with both color and shape in ggplot2. So, what I did is to repeat the same 4 shapes across the 16 different illness. Thus, I assigned "shape" and "color" to the variable Enfermedad (illness) and use scale_shape_manual. Again, it is not really what I wanted so if anyone has a better solution I would very much welcome it!

ggplot(EnfermGastro, aes(x=Año,y=Casos, shape=Enfermedad, colour=Enfermedad)) +

geom_point(size=2.5) +

scale_shape_manual(values =rep.int(c(16, 1, 18, 8),times=4)) +

scale_fill_manual(values= getPalette(colourCount)) +

scale_x_continuous( breaks=c(1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,
                        2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018)) +
scale_y_continuous(labels=comma) +

theme_bw() +
theme(axis.line=element_line(colour = "black"),
       panel.grid.major.y = element_line(colour="grey75", linetype="dotted"),
       panel.grid.major.x = element_line(colour="grey75", linetype="dotted"),
       panel.border=element_blank(),
       legend.background=element_rect(fill="grey80", colour="black"),
       legend.title = element_text(size= 10),
       legend.text = element_text(size= 8),
       legend.position="bottom") +

  facet_wrap(~Tipo, scale= "free")

enter image description here

Emil
  • 23
  • 3