简体   繁体   English

ggplot2中的stat_density2d()问题

[英]Problems with stat_density2d() in ggplot2

My dataset (origen) is composed by 3 columns, a factor variable (Origen), and two cuantitative variables (Ae and Adp). 我的数据集(起源)由3列,一个因子变量(Origen)和两个定量变量(Ae和Adp)组成。 Some factors of the origen categorical variable are composed by only 1 or 2 observations. 起源分类变量的某些因素仅由1或2个观察值组成。

When running this code: 运行此代码时:

ggplot (origen, aes (x = Ae, y = Adp, colour = Origen)) + 
stat_density2d(aes (fill = Origen, colour = Origen, alpha = 0.9),
geom = "polygon", lty = 3,lwd = 0.001) + xlim(8, 23) + ylim(22, 58) + 
scale_alpha(range = c(0, 0.2),guide = "none") + 
geom_point(aes(fill=Origen),colour="black",pch=21,size=3,alpha=0.1)

I get no results for the stat_density2d function within the ggplot() one. 我在ggplot()中没有获得stat_density2d函数的结果。 However, when I manually remove those groups which contain only 1 or 2 observations at most, the above code works properly and the density areas appear. 但是,当我手动删除那些最多仅包含1个或2个观测值的组时,上述代码可以正常工作,并且出现密度区域。

How can I avoid this issue by modifying the code? 如何通过修改代码来避免此问题? What new attributes must be writen in the stat_density2d function to run those more-than-two-observations groups as well as the points of the other groups? 必须运行stat_density2d函数哪些新属性才能运行那些大于两个观察值的组以及其他组的要点?

This is not the direct answer to your question, but you can remove the group with <1 observations by doing group_by() and filter() . 这不是您问题的直接答案,但是您可以通过执行group_by()filter()来删除观察值小于1的组。

library(dplyr)

data_filtered <- data %>%
  group_by(Origen) %>%
  filter(n() > 1)

(The warning "missing value where TRUE/FALSE needed" seems to be raised from here: https://github.com/tidyverse/ggplot2/blob/8778b48b37d8b7e41c0f4f213031fb47810e70aa/R/stat-density-2d.r#L58 ) (警告“缺少需要TRUE / FALSE的值”似乎是从这里引发的: https : //github.com/tidyverse/ggplot2/blob/8778b48b37d8b7e41c0f4f213031fb47810e70aa/R/stat-density-2d.r#L58

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM