我如何让geom_area()为缺失的值留下空隙? [英] How can I make geom_area() leave a gap for missing values?

查看:1255
本文介绍了我如何让geom_area()为缺失的值留下空隙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 geom_area()进行绘图时,我预计其执行很像 geom_bar(),但是我'

  require(dplyr)
require(ggplot2)$ b有点困惑于这种缺失值的行为。


$ set.seed(1)

test < - data.frame(x = rep(1:10,3),y = abs(rnorm(30)),z = rep(LETTERS [1:3],10))%>%arrange(x,z)

#我也不知道为什么geom_area需要首先对data.frame进行排序。

test [test $ x == 4,y] < - NA

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 < - level(test $ z)
for(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]
}

然后用 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$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]
}

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()为缺失的值留下空隙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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