I am trying to cheat a bit on the fact that ggplot2 does not support multiple axes (I know it's not ideally correct to use 2 axes).
I have a geom_point
and would like to add a geom_histogram
on top of it. The cheat here would be to somehow manipulate the 'Y' axis scaling to match the one from geom_point
.
geom_histogram
syntax goes something like this:
geom_histogram(data=df, aes(x=x1, y=..count..))
The interesting part for me is the 'y' axis in the aesthetics, this ..count..
(or ..density..
, ..ncount..
, etc) can be manipulated for example by doing this:
geom_histogram(data=df, aes(x=x1, y=..count..+100))
and this would add 100 to whatever was your count of variable x1 in each bin:
set.seed(1)
df <- data.frame(x1=rnorm(200))
ggplot(df)+
geom_histogram(aes(x=x1, y=..count..+100))
Now, when I try to do the same but replacing the '100' with a variable, I get an error:
var <- 100
ggplot(df)+
geom_histogram(aes(x=x1, y=..count..+var))
Error in eval(expr, envir, enclos) : object 'var' not found
I suppose this error comes because I am inside aes()
so my question would be: why is this happening and how can I use a variable to manipulate this 'y' axis?