How can I prevent stat density from interfering with my legend - R?

357 Views Asked by At
# Create data df and mapping df ----

maindf<-data.frame(x_coord=sample(1:100, 40, replace=T),
                   y_coord=sample(1:100, 40, replace=T),
                   action=sample(1:5, 40, replace=T),
                   ID=1:40)

mapping<-data.frame(vals=c(1,2,3,4,5),
                    cols=c("#990000","#994C00","#4C9900","#000099","#A0A0A0"),
                    desc=c("One","Two","Three","Four","Five"),
                    shape=c(20,15,17,17,20),size=c(5,5,5,6,5))

# Asssign levels ----
maindf$action<-as.factor(maindf$action)
vecPos<-match(levels(maindf$action),mapping$vals)
myColors1<-as.vector(mapping$cols[vecPos])
myShapes1<-as.vector(mapping$shape[vecPos])
mySize1<-as.vector(mapping$size[vecPos])
levels(maindf$action)<- mapping$desc[vecPos]

# Define colour, size and shape scales ----
colScale1 <- scale_colour_manual(name = "Action",values = myColors1)
shapeScale1 <- scale_shape_manual(name="Shape",values=myShapes1)
sizeScale1<-scale_size_manual(name="Size",values=mySize1)

# Heatmap ----
cols <- rev(brewer.pal(7, "Spectral"))

ggplot(data=maindf) + 
  stat_density2d(aes(x=as.numeric(x_coord),y=as.numeric(y_coord),fill=..density..), 
                 geom="tile",  contour = FALSE) + 
  scale_fill_gradientn(colours=cols,guide="none") + 
  theme_minimal() +
  geom_rect(aes(xmin = 0, xmax = 100, ymin = 0, ymax = 100), fill = NA, colour = "#000000", size = 1) +
  theme(rect = element_blank(), 
        line = element_blank())+
  geom_point(aes(x=as.numeric(x_coord), y=as.numeric(y_coord),
                 colour=action,size=action,shape=action)) +
  sizeScale1+colScale1+shapeScale1+
  labs(x = "", y = "")+guides(colour = guide_legend("Key"), size = guide_legend("Key"),
                              shape = guide_legend("Key"))+
  ylim("")

Could anyone please point me in the right direction to why stat-density-2d interferes with my legend icons. If you comment out the lines:

stat_density2d(aes(x=as.numeric(x_coord),y=as.numeric(y_coord),
                   fill=..density..), geom="tile",  contour = FALSE) + 
scale_fill_gradientn(colours=cols,guide="none") + 

then the key is integrated as desired. I also require the guide for the heatmap to be removed from the legend and just the integrated symbols to be present.

All help is greatly appreciated!

Cheers,

1

There are 1 best solutions below

1
On

Basically you just set show.legend=FALSE. Then you should get the desired output:

ggplot(data=maindf) + 
  stat_density2d(aes(x=as.numeric(x_coord),y=as.numeric(y_coord),fill=..density..), 
                 geom="tile",  contour = FALSE,show.legend = FALSE) + 
  scale_fill_gradientn(colours=cols,guide="none") + 
  theme_minimal() +
  geom_rect(aes(xmin = 0, xmax = 100, ymin = 0, ymax = 100), fill = NA, colour = "#000000", size = 1) +
  theme(rect = element_blank(), 
        line = element_blank())+
  geom_point(aes(x=as.numeric(x_coord), y=as.numeric(y_coord),
                 colour=action,size=action,shape=action)) +
  sizeScale1+colScale1+shapeScale1+
  labs(x = "", y = "")+guides(colour = guide_legend("Key"), size = guide_legend("Key"),
                              shape = guide_legend("Key"))+
  ylim("")

Output looks like this: enter image description here