I'll begin by loading some data for a reprex.
library(fivethirtyeight) library(tidyverse) data("biopics") df <- biopics %>% filter(!is.na(box_office))
I'd like to have two plots appear in the same plot area (not faceted or aligned, but actually one atop the other).
The first being a density plot of
The second being a plot of
year_release as a scatter plot.
log10(box_office) has a range of ~2 to ~10
the density of
year_release has a range of 0 to ~0.4
My attempts to plot the two on the same time plot have been using the secondary axis functionality.
df %>% ggplot() + geom_density(aes(x = year_release)) + geom_point(aes(x = year_release, y = log10(box_office))) + scale_y_continuous(sec.axis = sec_axis(~. * 20, name = "log10(box_office)"))
I chose to scale the secondary axis up by a factor of 20 because
0.5 * 20 = 10, which is around the maximum value of
But this doesn't seem to produce anything like the desired effect.
Any thoughts for what I could do differently?