为连续数据映射添加NA值到ggplot图例 [英] Add NA value to ggplot legend for continuous data map

查看:351
本文介绍了为连续数据映射添加NA值到ggplot图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ggplot将数据值映射到(强化的)SpatialPolygonsDataFrame,但许多多边形都有NA值,因为没有可用的数据。



我使用na.value =white来正确显示缺失的数据,但是我想在图例中添加一个带白色填充的框(或

  library(ggplot2)

india.df < - read.csv('india.df.csv')
#(我不知道如何提供此文件以使代码可重现)

ggplot()+
geom_polygon(data = india.df,aes(x = long,y = lat,group = group,fill = Area_pct))+
scale_fill_gradient(low =orange2,high =darkblue,na (数据=印度.df,aes_string(x = x,y = y,组=组),颜色=灰色,大小= 0.25)+
theme_bw()+
coord_map()+
labs(title =古吉拉特邦灌溉稻米 - 2001,
fill =面积(%))

(我有一个很好的图像来说明这一点,但没有足够的信誉点来发布它)

我读过这个,但我的数据是连续的(不是离散的),并且这个,但我无法弄清楚如何调整'line'变为'fill'。



感谢您的帮助!

解决方案

p>

  data [is.na(data)] < -  0 

这样,您的nas将被替换为零,而yout legend将显示0s。



并向我们展示图片你可以有一个博客,并可以粘贴链接在这里


I'm using ggplot to map data values to a (fortified) SpatialPolygonsDataFrame, but many of the polygons have NA values because there is no data available.

I used na.value = "white" to display the missing data correctly, but I'd like to add a box with a white fill in the legend (or a separate legend) with the label "no data".

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 (%)")

(I have a great image to illustrate this but don't have enough reputation points to post it)

I've read this, but my data is continuous (not discrete), and this, but I can't figure out how to adapt the 'line' change to 'fill'.

Thanks for the help!!

解决方案

you can replace your NAs with 0 using

data[is.na(data)] <- 0

that way your nas will be replaced by zero and yout legend will show "0s"

And to show us the image you can have a blog and can paste the link here

这篇关于为连续数据映射添加NA值到ggplot图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆