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")