本文介绍了你能否让geom_ribbon为缺失的值留下空隙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在跟踪开始的讨论:
I am following up a discussion started at: How can I make geom_area() leave a gap for missing values?. It seems like geom_ribbon is not longer leaving gaps for missing values. Please try to execute the reproducible example in the attached link. I cannot get the answer described. Can you?
解决方案
Looks like a bug in ggplot2
, there seems to be a missing handle_na
function that needs to be added as a part of a new unified way of dealing with NA values.
Update:
The first post here refined an entire new ggproto
to fix this, but I realizedthat as a one-liner workaround you can just override the handle_na
function like I do in the code below (# fix GeomRibbon
):
require(dplyr)
require(ggplot2)
require(grid)
set.seed(1)
test <- data.frame(x = rep(1:10, 3), y = abs(rnorm(30)), z = rep(LETTERS[1:3], 10))
%>% arrange(x, z)
test[test$x == 4, "y"] <- NA
test$ymax <- test$y
test$ymin <- 0
zl <- levels(test$z)
for (i in 2:length(zl)) {
zi <- test$z == zl[i]
zi_1 <- test$z == zl[i - 1]
test$ymin[zi] <- test$ymax[zi_1]
test$ymax[zi] <- test$ymin[zi] + test$ymax[zi]
}
# fix GeomRibbon
GeomRibbon$handle_na <- function(data, params) { data }
ggplot(test, aes(x = x,y=y, ymax = ymax, ymin = ymin, fill = z)) +
geom_ribbon() +
scale_x_continuous(breaks = 1:10)
yielding:
这篇关于你能否让geom_ribbon为缺失的值留下空隙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!