我正在使用ggplot将数据值映射到(强化的)SpatialPolygonsDataFrame,但是许多多边形都有NA值,因为没有可用的数据.
我使用na.value ="white"来正确显示缺失的数据,但我想在图例中添加带有白色填充的框(或单独的图例),标签为"无数据".
library(ggplot2)
india.df <- read.csv('india.df.csv')
# (I don't know how to provide this file to make the code reproducible)
ggplot() +
geom_polygon(data=india.df, aes(x = long, y = lat, group = group, fill=Area_pct)) +
scale_fill_gradient(low="orange2", high="darkblue", na.value = "white") +
geom_path(data=india.df, aes_string(x = x, y = y, group = group), color = "gray", size = 0.25) +
theme_bw() +
coord_map() +
labs(title = "Rice Under Irrigation in Gujarat - 2001",
fill = "Area (%)")
Run Code Online (Sandbox Code Playgroud)
(我有一个很好的图像来说明这一点,但没有足够的声誉点来发布它)
我有分类数据,我想映射使用热图(geom_tile)的频率,就像下面的示例一样:
data("mtcars")
freq <- data.frame(xtabs(~cyl + gear, mtcars)) #count number of 4,6,8 cyl cars by gear
ggplot(freq, aes(cyl, gear)) +
geom_tile(aes(fill = Freq)) +
scale_fill_gradient(low = "white",high = "steelblue")
Run Code Online (Sandbox Code Playgroud)

但我想根据显着或不显着结果(0-1 值)的比例分割每个图块。在此示例中,我将生成相同的频率计数,但区分自动传输和手动传输 ( am)
freq_am <- data.frame(xtabs(~cyl + gear + am, mtcars))
print(freq_am)
#cyl gear am Freq
4 3 0 1
6 3 0 2
8 3 0 12
4 4 0 2
6 4 0 2
8 4 0 0
4 5 0 0
6 5 0 0 …Run Code Online (Sandbox Code Playgroud)