【发布时间】:2015-05-22 22:56:00
【问题描述】:
我正在使用 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 (%)")
(我有一张很棒的图片来说明这一点,但没有足够的声望点来发布它)
我读过this,但我的数据是连续的(不是离散的)和this,但我不知道如何将“线”更改为“填充”。
感谢您的帮助!
【问题讨论】:
-
发现 github.com/hadley/ggplot2/issues/410 似乎我们陷入了变通办法......
-
如果您根据您的问题制作一个可重复的最小示例,我们更有可能为您提供帮助。我们可以使用的东西向您展示如何解决您的问题。您可以查看this SO post,了解如何在 R 中制作一个出色的可重现示例。