# 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("")
谁能指出我为什么 stat-density-2d 会干扰我的图例图标的正确方向。如果您注释掉这些行:
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") +
然后根据需要集成密钥。我还需要从图例中删除热图指南,并且只显示集成符号。
非常感谢所有帮助!
干杯,