Reputation: 447
I'm looking at passing transform
into the below function to be able to line up the secondary axis... however, I get an error:
Error in y * transform : non-numeric argument to binary operator
library(ggplot2)
a.samples <- rbeta(10000, 25, 75)
b.samples <- rbeta(10000, 35, 65)
df <- data.frame(x = b.samples/a.samples)
p <- ggplot(df, aes(x = x)) + geom_histogram(fill = 'grey50', alpha = .6)
transform <- max(ggplot_build(p)$data[[1]]$ymax)
p + geom_line(aes(y = ..y.. * transform), stat='ecdf') + # error here
geom_vline(xintercept = 1, linetype = 'dashed') +
scale_y_continuous(sec.axis = sec_axis(~./transform, name = "ecdf")) +
guides(color = FALSE)
If I explicitly write the value 1260
into the aes argument, it works.
...geom_line(aes(y = ..y.. * 1260), stat='ecdf')...
Upvotes: 1
Views: 406
Reputation: 176
So your above code oddly worked for me without any other errors other than the binwidths being off so I don't know what was generating the error but I just adjusted it with adding stat_ecdf()
library(ggplot2)
a.samples <- rbeta(10000, 25, 75)
b.samples <- rbeta(10000, 35, 65)
df <- data.frame(x = b.samples/a.samples)
p <- ggplot(df, aes(x = x)) + geom_histogram(fill = 'grey50', alpha = .6)
transform <- max(ggplot_build(p)$data[[1]]$ymax)
transform
p + stat_ecdf(aes(y = ..y.. * transform),geom = "line") +
geom_vline(xintercept = 1, linetype = 'dashed') +
scale_y_continuous(sec.axis = sec_axis(~./transform, name = "ecdf")) +
guides(color = FALSE)
Upvotes: 1
Reputation: 447
Was able to get this to work with aes_string
p + geom_line(aes_string(y = paste0('..y.. * ', transform)), stat='ecdf') +
geom_vline(xintercept = 1, linetype = 'dashed') +
scale_y_continuous(sec.axis = sec_axis(~./transform, name = "ecdf")) +
guides(color = FALSE)
Upvotes: 1