需要直方图上的平滑线。为什么 geom_density 画两条线?

Need smoothed line on histogram. Why does geom_density draw two lines?

我的直方图有问题,我想在其上添加一条平滑线,最好是多项式平滑线。

我得到的最接近的是添加一个平滑的 geom_density,或 geom_linestat="density" 以及额外的调整选项。

无论如何,在使用下面的简化代码时,我的密度也有两行。这是为什么?我只想要一条平滑的密度线或多项式平滑。

示例数据可在此处找到:https://www.dropbox.com/s/13795hnpqmwftnq/chyp3histo_total_lip.rda?dl=0

使用此代码:

histos1a<-ggplot(data=chyp3histo_total_lip,aes(x=totallipid))+
 geom_histogram(color=col,fill=col)+
 geom_histogram(fill="white")+ 
 geom_density()

给出:

为什么会画出两条看起来像范围的线?

干杯,谢谢!

问题是您有 grouped tibble;显然(令我惊讶的是)ggplot2 注意到了这一点。尝试

cc <- dplyr::ungroup(chyp3histo_total_lip)
ggplot(data=cc,...) ## etc.

如果您发布了 str(chyp3histo_total_lip) 的结果,我们就可以在不下载数据的情况下进行诊断...

str(chyp3histo_total_lip)
Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 544 obs. of  1 variable:
 $ totallipid: num  NA 0.046 NA NA NA ...
 - attr(*, "vars")= chr  "haul" "pred"
 - attr(*, "drop")= logi TRUE
 - attr(*, "indices")=List of 2
  ..$ : int  0 1 2 3 4 5 6 7 8 9 ...
  ..$ : int  122 123 124 125 126 127 128 129 130 131 ...
 - attr(*, "group_sizes")= int  244 300
 - attr(*, "biggest_group_size")= int 300
 - attr(*, "labels")='data.frame':  2 obs. of  2 variables:
  ..$ haul: Factor w/ 2 levels "17:47h","1:44h": 1 2
  ..$ pred: Factor w/ 12 levels "C. glacialis C3",..: 5 5
  ..- attr(*, "vars")= chr  "haul" "pred"
  ..- attr(*, "drop")= logi TRUE