lokheart lokheart - 2 months ago 18
R Question

Plot with 2 y axes, one y axis on the left, and another y axis on the right

I need to plot a bar chart showing counts and a line chart showing rate all in one chart, I can do both of them separately, but when I put them together, I scale of the first layer (i.e. the

geom_bar
) is overlapped by the second layer (i.e. the
geom_line
).

Can I move the axis of the
geom_line
to the right?

Answer

<rant> Sometimes a client wants two y scales. Giving them the "flawed" speech is often pointless. But I do like the ggplot2 insistence on doing things the right way. I am sure that ggplot is in fact educating the average user about proper visualization techniques. </rant>

Maybe you can use faceting and scale free to compare the two data series? - e.g. look here: https://github.com/hadley/ggplot2/wiki/Align-two-plots-on-a-page