如何从ggplot2中的facet_wrap中删除NA? [英] How to remove NA from facet_wrap in ggplot2?

查看:66
本文介绍了如何从ggplot2中的facet_wrap中删除NA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用facet_wrap在ggplot2中制作多边形地图.我的变量"crop"中有两个因子水平(大豆,玉米).但是,我得到了三个图:大豆,玉米和一个具有NA值的图.此外,前两个方面均不显示NA值-

I am trying to use facet_wrap to make a polygon map in ggplot2. I have two factor levels (soybean, Maize) in my variable "crop" However, I am getting three plots: soybean, maize and one with NA values. In addition NA values are not displayed in the first two facets-

这是我制作地图的代码:

here is my code to make the map:

ggplot(study_area.map, aes(x=long, y=lat, group=group)) + 
  geom_polygon(aes(fill=brazil_loss_new2)) + 
  geom_path(colour="black") + 
  facet_wrap(~crop, ncol=2, drop=T) + 
  scale_fill_brewer(na.value="grey", palette="Blues", 
    name="Average production lossess\n per municipality", 
    breaks = levels(study_area.map$brazil_loss_new2), 
    labels = levels(study_area.map$brazil_loss_new2)) + 
  theme() + 
  coord_fixed()

这就是我得到的:

如果我使用na.omit,我会得到下图(虽然更好,但是前两个图中仍然缺少NA值)

If I use na.omit I get the following figure (which is better, but there are still the NA values missing in the first two plots)

在此处输入图片描述

无论变量是否为NA,都为每个变量和市镇添加行,最终解决了该问题.这是我要找的东西:

Including rows for each variable and municipality no matter if the variable of interest is NA or not, finally solves the problem. Here is what I was looking for:

具有NA值的市政当局的大量损失

推荐答案

您可以在调用ggplot函数时删除不适用的NA.删除核心数据功能中的NA.这样就不会画出他们

You can remove the NA's in place while calling the ggplot function. Remove the NA's in the core data function. That way it wont plot them

ggplot(data = study_area.map[!(is.na(study_area.map[$brazil_loss_new2)),], aes(x=long, y=lat, group=group))+ 
geom_polygon(aes(fill=brazil_loss_new2))+ 
geom_path(colour="black")+ facet_wrap(~crop, ncol=2, drop=T)+ scale_fill_brewer(na.value="grey", palette="Blues", name="Average production lossess\n per municipality", breaks =levels(study_area.map$brazil_loss_new2), labels=levels(study_area.map$brazil_loss_new2))+ 
theme()+ 
coord_fixed()

这篇关于如何从ggplot2中的facet_wrap中删除NA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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