Reputation: 395
I have an issue with my histogram on top of which I want to add a smoothed line, preferably a polynomial smooth.
The closest I got was adding a smoothed geom_density
, or geom_line
with stat="density"
with additional adjust option.
In any case, I am getting two lines for my density, also when using the simplified code below. Why is that? I only want one smoothed density line or polynomial smooth.
Example data can be found here: https://www.dropbox.com/s/13795hnpqmwftnq/chyp3histo_total_lip.rda?dl=0
Using this code:
histos1a<-ggplot(data=chyp3histo_total_lip,aes(x=totallipid))+
geom_histogram(color=col,fill=col)+
geom_histogram(fill="white")+
geom_density()
Why does it draw the two lines which seem like a range?
Cheers and thanks!
Upvotes: 1
Views: 3215
Reputation: 226532
The problem is that you have a grouped tibble; apparently (to my surprise) ggplot2
pays attention to this. Try
cc <- dplyr::ungroup(chyp3histo_total_lip)
ggplot(data=cc,...) ## etc.
If you had posted the results of str(chyp3histo_total_lip)
we would have been able to diagnose this without downloading the data ...
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
Upvotes: 3