geom_density:删除水平线为零

时间:2019-09-05 10:58:00

标签: r ggplot2

是否有一种方法可以删除geom_density的水平线为零?试图同时绘制两条重叠的密度线时,该线看起来不太漂亮。

演示示例可从此处获取:

http://www.sthda.com/english/wiki/ggplot2-density-plot-quick-start-guide-r-software-and-data-visualization

set.seed(1234)
df <- data.frame(
  sex=factor(rep(c("F", "M"), each=200)),
  weight=round(c(rnorm(200, mean=55, sd=5),
                 rnorm(200, mean=65, sd=5)))
  )

library(ggplot2)
# Basic density
p <- ggplot(df, aes(x=weight)) + geom_density()

1 个答案:

答案 0 :(得分:3)

是的,您必须使用density stat函数并将geom更改为line(可以选择将位置更改为"identity"

ggplot(iris, aes(Sepal.Length, colour = Species)) +
  stat_density(geom = "line", position = "identity")

enter image description here