[英]How to remove NA from facet_wrap in ggplot2?
我正在尝试使用 facet_wrap 在 ggplot2 中制作多边形地图。 我的变量“作物”中有两个因子水平(大豆、玉米)但是,我得到了三个图:大豆、玉米和一个具有 NA 值的图。 此外,前两个方面不显示 NA 值-
这是我制作地图的代码:
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 值)
无论感兴趣的变量是否为 NA,都包括每个变量和自治市的行,最终解决了问题。 这是我一直在寻找的:
您可以在调用 ggplot 函数时删除 NA。 删除核心数据函数中的 NA。 这样它就不会绘制它们
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()
在数据调用中包含na.omit()
是否na.omit()
您的需求?
ggplot(na.omit(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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.