r - Plot multiple group histogram with overlaid line ggplot -


i'm trying plot multiple group histogram overlaid line, cannot right scaling histogram. example:

  ggplot() + geom_histogram(data=df8,aes(x=log(y),y=..density..),binwidth=0.15,colour='black') +  geom_line(data = as.data.frame(pdf8), aes(y=pdf8$f,x=pdf8$x), col = "black",size=1)+theme_bw() 

produces right scale. when try perform fill according groups, each group scaled separately.

ggplot() + geom_histogram(data=df8,aes(x=log(y),fill=vec8,y=..density..),binwidth=0.15,colour='black') +    geom_line(data = as.data.frame(pdf8), aes(y=pdf8$f,x=pdf8$x), col = "black",size=1)+theme_bw() 

picture

how scale black line overlaid on histogram , on y axis density?

it going difficult others without reproducible example, perhaps you're after:

library(ggplot2)  ggplot(data = mtcars, aes(x = mpg, fill = factor(cyl))) +   geom_histogram(aes(y = ..density..)) +   geom_line(stat = "density") 

r plot

if rather density line pertain entire dataset, need move fill aesthetic geom_histogram function:

ggplot(data = mtcars, aes(x = mpg)) +   geom_histogram(aes(y = ..density.., fill = factor(cyl))) +   geom_line(data = mtcars, stat = "density") 

r plot 2


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -