1
# 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,

5th
  • 2,097
  • 3
  • 22
  • 41
Matt
  • 45
  • 4
  • Are you a beginner in R? Do you not know how to read the documentation? Just be careful next time you ask a question. There is no shame in admitting that you are new or worked too hard on the problem. – 5th Aug 10 '18 at 18:19
  • @5th There's no real point in having question askers state things like "I am a beginner in R". "Admitting" that they are new, or rusty, or whatever else doesn't add value to the question. It is self-evident from the question to the extent that it is relevant. – Gregor Thomas Aug 10 '18 at 18:22
  • @Gregor I am mainly asking out of concern and because the twist of such a complicated code problem and such a neat solution really baffled me. I have seen questions here where you got downvoted for just asking a question which had an obvious solution. – 5th Aug 10 '18 at 18:35
  • @5th I am actually a self taught R coder... I do research my problems a lot before posting (hence being able to write a complexish code without problems). However sometimes the solution just isn't so straight forward! If you have any tips in which my code could be cleaner I would be more than grateful. Cheers, – Matt Aug 10 '18 at 20:22

1 Answers1

1

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

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
5th
  • 2,097
  • 3
  • 22
  • 41