Skip to content

Instantly share code, notes, and snippets.

@nozma
Last active January 22, 2017 14:16
Show Gist options
  • Select an option

  • Save nozma/03fcabb99052b02ea8ae72aa38321167 to your computer and use it in GitHub Desktop.

Select an option

Save nozma/03fcabb99052b02ea8ae72aa38321167 to your computer and use it in GitHub Desktop.
ggplot2における2軸プロットの使いみち ref: http://qiita.com/nozma/items/c50a8d45c73bf8b84794
library(ggplot2)
library(dplyr)
# hwy : highway miles per gallon
# 1mile == 1.60934km
# 1gallon == 3.78541L
# ∴ mile / gallon = 1.60934 / 3.78541 km / L
mpg2kpl <- function(x) x * 1.60934 / 3.78541
mpg %>%
ggplot(aes(displ, hwy)) +
geom_point() +
scale_y_continuous(sec.axis = sec_axis(~ mpg2kpl(.), name = "km/L")) +
labs(y = "mile/gal") +
theme_classic()
# temperature: deg C
# pressure: mm of Hg
# 1 mmHg == 0.133322 kPa
mmHg2kPa <- function(x) x * .133322
C2K <- function(x) x + 273.15
pressure %>%
ggplot(aes(temperature, pressure)) +
geom_point() +
scale_x_continuous(sec.axis = sec_axis(~ C2K(.), name = "Temperature (K)")) +
scale_y_continuous(sec.axis = sec_axis(~ mmHg2kPa(.), name = "Pressure (kPa)")) +
labs(x = "Temperature (C)", y = "Pressure (mm of Hg)") +
theme_classic()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment