Skip to content

Instantly share code, notes, and snippets.

@vicperotti
Last active February 3, 2021 13:19
Show Gist options
  • Save vicperotti/31793314934fe9dc221268f94476f9b7 to your computer and use it in GitHub Desktop.
Save vicperotti/31793314934fe9dc221268f94476f9b7 to your computer and use it in GitHub Desktop.
GGplot dual axis bar and line graph, with coeff to adjust scale between axes
coeff <-10000 #the factor to divide one access
ggplot(data=hdata2,mapping=aes(x=YearBuilt)) +
geom_bar() +
geom_line(mapping=aes(x=YearBuilt,y=SalePrice/coeff),stat="summary",fun="mean",color="blue",size=1) +
scale_y_continuous(
# Features of the first axis
name = "Number of Sales",
# Add a second axis and specify its features
# The labels piece creates whole integers rather than scientific notation
sec.axis = sec_axis(~.*coeff, name="Mean Price",labels=function(n){format(n, scientific = FALSE)}),
)
@vicperotti
Copy link
Author

@vicperotti
Copy link
Author

Rplot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment