ggplot在改变xlim和ylim时不能正确显示密度曲线。

问题描述 投票:0回答:1

请看下面的图。

library(ggplot2)
dat <- data.frame(x = rnorm(1e6, sd = 0.01))
ggplot(data = dat, aes(x = x)) + geom_density() + ylim(0, 1) + xlim(-5, 5)

enter image description here

~密度> 0.2的密度曲线有相当一部分缺失。

有谁知道一个变通的办法吗?

r ggplot2
1个回答
1
投票

你必须使用 coord_cartesian 以保留所有的基础数据点。

library(ggplot2)
dat <- data.frame(x = rnorm(1e6, sd = 0.01))
ggplot(data = dat, aes(x = x)) + geom_density() +
  coord_cartesian(xlim = c(-5, 5), ylim = c(0, 1)) 

于2020-04-27创建。重读包 (v0.3.0)

© www.soinside.com 2019 - 2024. All rights reserved.