2016-05-12 109 views
2

我正在使用空間像素數據框在ggplot中繪製物種預測圖。我的情節幾乎是我想要的,唯一的問題是我的光柵比例從白色變成了紅色,所以很難看到它在傳說see the plot中的起始位置。我想繪製一個框來概述傳說的關鍵 - 只是從白色到紅色的酒吧不再是其餘的酒吧。我一直在尋找一個多小時,但我找不到任何方法來做到這一點,只有這樣才能給圖例添加一個背景或者在圖例文本和圖標周圍繪製一個框,我不想這樣做。有誰知道這是可能做到ggplot或我需要改變我的顏色漸變?圍繞光柵鍵繪製一個框

謝謝!

我的繪製代碼:

ggplot() + 
    geom_raster(data=habs_pop_clip1, aes(x = easting.x, y = northing.x, fill = pred)) + 
    scale_fill_gradient("Probability of occurrence", low="white", high="red",limits = c(0,1)) + 
    coord_fixed(ratio=1, xlim=c(545000, 654000), ylim=c(278000,347000))+ 
    geom_polygon(data=Norfolk1, aes(x=long, y=lat, Group=group), color="grey",fill=NA)+ 
    theme(text = element_text(family = "serif")) + 
    geom_segment(aes(x = 550000, y = 278500, xend = 560000, yend = 278500), lineend = "round") + 
    annotate("text", x = 555000, xend = 555000, y = 282000, yend = 282000, label = "10 km", family = "serif", size = 4) + 
    ggtitle(colnames(bat_occ[i+7])) + 
    theme(plot.margin=unit(c(0.5, 0.5, 0.5, 0.5), "cm")) + 
    theme_bw() + 
    theme(axis.line = element_line(colour = "black"), 
     panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank(), 
     panel.border = element_blank(), 
     panel.background = element_blank(), 
     axis.line=element_blank(), 
     axis.ticks=element_blank(), 
     axis.title.x=element_blank(), 
     axis.title.y=element_blank(), 
     axis.text =element_blank()) 

回答

2

我不知道是否有一個內置的參數。如果沒有,你可以手動添加一個矩形,

df <- reshape2::melt(outer(1:4, 1:4), varnames = c("X1", "X2")) 

p <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))+ scale_fill_continuous(guide = "colorbar") 


g <- ggplotGrob(p) 
g$grobs[[8]][[1]][[1]] <- gtable::gtable_add_grob(g$grobs[[8]][[1]][[1]], 
                rectGrob(gp=gpar(fill=NA, lwd=5)), 4, 2) 
grid.newpage() 
grid.draw(g) 

enter image description here

+0

驚人!謝謝!這正是我想要的。 –