本文介绍了我如何让geom_area()为缺失的值留下空隙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 当我使用 geom_area()进行绘图时,我预计其执行很像 geom_bar(),但是我' require(dplyr) require(ggplot2)$ b有点困惑于这种缺失值的行为。 $ set.seed(1) test #我也不知道为什么geom_area需要首先对data.frame进行排序。 test [test $ x == 4,y] ggplot(test,aes(x,y,fill = z))+ geom_bar (stat =identity,position =stack) 生成此堆积条形图。 但是,如果我更改为stack_area(),则会插入缺少的值。 > ggplot(test,aes(x,y,fill = z))+ geom_area(stat =identity,position =stack)警告消息:删除了3行包含缺失值(position_stack) 。 na.rm = FALSE 或 c $ c> na.rm = TRUE 它没有区别。 ggplot(test,aes(x ,y,fill = z))+ geom_area(stat =identity,position =stack,na.rm = TRUE)警告信息:删除3行,包含缺失值(position_stack) ggplot(test,aes(x,y,fill = z))+ geom_area(stat =identity ,position =stack,na.rm = FALSE)警告消息:删除了3行包含缺失值(position_stack)。 显然,无论我尝试的是不是工作。我怎样才能在 stack_area()? 解决方案 然而, geom_ribbon ,其中 geom_area 是一种特殊情况,缺少缺失值。 geom_ribbon 也会绘制一个区域,但您必须指定最大和最小y值。所以这个技巧可以通过手动计算这些值来完成,然后用 geom_ribbon()进行绘图。从数据框 test 开始,我创建了 ymin 和 ymax 数据如下: test $ ymax< - 测试$ y 测试$ ymin< - 0 zl for(in 2:length(zl)){ zi zi_1 test $ ymin [zi]< - test $ ymax [zi_1] test $ ymax [zi]< - test $ ymin [zi] + test $ ymax [zi] } 然后用 geom_ribbon : ggplot(test,aes(x = x,ymax = ymax = ymin,fill = z))+ geom_ribbon() 给出如下图: When I plot using geom_area() I expect it to perform a lot like geom_bar(), but I'm a little perplexed by this behavior for missing values. require(dplyr) require(ggplot2) set.seed(1) test <- data.frame(x=rep(1:10,3), y=abs(rnorm(30)), z=rep(LETTERS[1:3],10)) %>% arrange(x,z)# I also have no idea why geom_area needs the data.frame to be sorted first. test[test$x==4,"y"] <- NA ggplot(test, aes(x, y, fill=z)) + geom_bar(stat="identity", position="stack")Produces this stacked bar chart.However, if I change to stack_area() it interpolates across the missing values.> ggplot(test, aes(x, y, fill=z)) + geom_area(stat="identity", position="stack")Warning message:Removed 3 rows containing missing values (position_stack).If I add in na.rm=FALSE or na.rm=TRUE it makes no difference. ggplot(test, aes(x, y, fill=z)) + geom_area(stat="identity", position="stack", na.rm=TRUE) Warning message: Removed 3 rows containing missing values (position_stack) ggplot(test, aes(x, y, fill=z)) + geom_area(stat="identity", position="stack", na.rm=FALSE) Warning message: Removed 3 rows containing missing values (position_stack).Obviously, whatever I'm trying isn't working. How can I show a gap in the series with stack_area()? 解决方案 It seems that the problem has to do with how the values are stacked. The error message tells you that the rows containing missing values were removed, so there is simply no gap present in the data that your are plotting.However, geom_ribbon, of which geom_area is a special case, leaves gaps for missing values. geom_ribbon plots an area as well, but you have to specify the maximum and minimum y-values. So the trick can be done by calculating these values manually and then plotting with geom_ribbon(). Starting with your data frame test, I create the ymin and ymax data as follows:test$ymax <-test$ytest$ymin <- 0zl <- 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]}and then plot with geom_ribbon:ggplot(test, aes(x=x,ymax=ymax,ymin=ymin, fill=z)) + geom_ribbon()This gives the following plot: 这篇关于我如何让geom_area()为缺失的值留下空隙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 20:20