Use parameter sec.axis
of scale_y_continuous()
sapply(c("pipeR", "ggplot2", "readr", "lubridate"), require, character.only = TRUE)
要求されたパッケージ pipeR をロード中です
要求されたパッケージ ggplot2 をロード中です
要求されたパッケージ readr をロード中です
要求されたパッケージ lubridate をロード中です
次のパッケージを付け加えます: ‘lubridate’
以下のオブジェクトは ‘package:base’ からマスクされています:
date
pipeR ggplot2 readr lubridate
TRUE TRUE TRUE TRUE
Temperature and precipitation in Naha city, Okinawa, Japan (2015)
d <- read_csv("data/ggplot2-two-axis/naha.csv", skip = 5, col_names = c("Date", "Temperature", "Precipitation"), col_types = cols(col_date(format = "%Y/%m"), "d", "_", "_", "d", "_", "_", "_"), locale = locale(encoding = "SJIS"))
head(d)
Precipitation
d %>>% ggplot() +
geom_bar(mapping = aes(x = Date, y = Precipitation), stat = "identity") +
ylab("Precipitation (mm)")
Temperature
d %>>% ggplot() +
geom_point(mapping = aes(x = Date, y = Temperature)) +
geom_line(mapping = aes(x = Date, y = Temperature)) +
ylab(expression("Temperature ("~degree~"C)"))
Range of data
summary(d)
Date Temperature Precipitation
Min. :2015-01-01 Min. :16.60 Min. : 22.00
1st Qu.:2015-03-24 1st Qu.:19.82 1st Qu.: 46.88
Median :2015-06-16 Median :24.35 Median : 84.00
Mean :2015-06-16 Mean :23.59 Mean :118.75
3rd Qu.:2015-09-08 3rd Qu.:28.02 3rd Qu.:124.38
Max. :2015-12-01 Max. :29.00 Max. :369.00
To set Temperature 0 – 30 and Precipitation 0 – 400, scale Precipitation by multiplying 30 / 400
to fit range of Temperature
gp1 <- d %>>% ggplot() +
geom_bar(mapping = aes(x = Date, y = Precipitation * 30 / 400), stat = "identity") +
geom_point(mapping = aes(x = Date, y = Temperature)) +
geom_line(mapping = aes(x = Date, y = Temperature)) +
scale_y_continuous(name = expression("Temperature ("~degree~"C)"), limits = c(0, 30))
gp1
Scale first Y axis by multiplying 400 / 300
to create secondary Y axis for Precipitation
scale_y_continuous(sec.axis = sec_axis(~ . * 400 / 30))
gp1 <- gp1 %+% scale_y_continuous(name = expression("Temperature ("~degree~"C)"), sec.axis = sec_axis(~ . * 400 / 30 , name = "Precipitation (mm)"), limits = c(0, 30))
Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale.
gp1
Final plot
d %>>% ggplot() +
geom_bar(mapping = aes(x = Date, y = Precipitation * 30 / 400), stat = "identity", colour = gray(0.5), fill = gray(0.5)) +
geom_line(mapping = aes(x = Date, y = Temperature)) +
geom_point(mapping = aes(x = Date, y = Temperature), size = 3, shape = 21, fill = "white") +
scale_x_date(name = "Month", breaks = seq.Date(as.Date("2015-01-01"), as.Date("2015-12-31"), by = "1 month"), labels = function(date){return(month(date, label = TRUE))}) +
scale_y_continuous(
name = expression("Temperature ("~degree~"C)"),
sec.axis = sec_axis(~ . * 400 / 30 , name = "Precipitation (mm)"),
limits = c(0, 30)) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
Temperature and precipitation in Kushiro city, Hokkaido, Japan (2015)
d2 <- read_csv("data/ggplot2-two-axis/kushiro.csv", skip = 5, col_names = c("Date", "Temperature", "Precipitation"), col_types = cols(col_date(format = "%Y/%m"), "d", "_", "_", "d", "_", "_", "_"), locale = locale(encoding = "SJIS"))
head(d2)
Precipitation
d2 %>>% ggplot() +
geom_bar(mapping = aes(x = Date, y = Precipitation), stat = "identity") +
ylab("Precipitation (mm)")
Temperature
d2 %>>% ggplot() +
geom_point(mapping = aes(x = Date, y = Temperature)) +
geom_line(mapping = aes(x = Date, y = Temperature)) +
ylab(expression("Temperature ("~degree~"C)"))
summary(d2)
Date Temperature Precipitation
Min. :2015-01-01 Min. :-3.300 Min. : 37.00
1st Qu.:2015-03-24 1st Qu.: 1.300 1st Qu.: 56.00
Median :2015-06-16 Median : 7.950 Median : 82.75
Mean :2015-06-16 Mean : 7.742 Mean : 96.50
3rd Qu.:2015-09-08 3rd Qu.:13.725 3rd Qu.:107.75
Max. :2015-12-01 Max. :18.400 Max. :242.00
To set Temperature -5 – 20 and Precipitation 0 – 250: * Scale Precipitation by multiplying 1/10
to fit range of Temperature, after that, scale Precipitation by adding -5
* Scale first Y axis by adding +5
, after that, scale Precipitation by multiplying 10
to create second Y axis for Precipitation
gp2 <- d2 %>>% ggplot() +
geom_bar(mapping = aes(x = Date, y = Precipitation / 10 - 5), stat = "identity", colour = gray(0.5), fill = gray(0.5)) +
geom_line(mapping = aes(x = Date, y = Temperature)) +
geom_point(mapping = aes(x = Date, y = Temperature), size = 3, shape = 21, fill = "white") +
scale_x_date(name = "Month", breaks = seq.Date(as.Date("2015-01-01"), as.Date("2015-12-31"), by = "1 month"), labels = function(date){return(month(date, label = TRUE))}) +
scale_y_continuous(
name = expression("Temperature ("~degree~"C)"),
sec.axis = sec_axis(~ (. + 5) * 10 , name = "Precipitation (mm)"),
limits = c(-5, 20)) +
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
gp2
geom_bar()
start from 0
, use geom_segment()
gp2$layers[[1]] <- geom_segment(mapping = aes(x = Date, y = Precipitation / 10 - 5, xend = Date, yend = -5), size = 11, lineend = "butt", colour = gray(0.5))
gp2
devtools::session_info()
Session info ----------------------------------------------------------------------------------------------------------
setting value
version R version 3.3.2 (2016-10-31)
system x86_64, mingw32
ui RStudio (1.0.44)
language (EN)
collate Japanese_Japan.932
tz Asia/Tokyo
date 2016-11-29
Packages --------------------------------------------------------------------------------------------------------------
package * version date source
assertthat 0.1 2013-12-06 CRAN (R 3.2.1)
colorspace 1.2-7 2016-10-11 CRAN (R 3.3.2)
devtools 1.12.0 2016-06-24 CRAN (R 3.3.1)
digest 0.6.10 2016-08-02 CRAN (R 3.3.1)
ggplot2 * 2.2.0 2016-11-11 CRAN (R 3.3.2)
gtable 0.2.0 2016-02-26 CRAN (R 3.2.5)
knitr 1.15 2016-11-09 CRAN (R 3.3.2)
labeling 0.3 2014-08-23 CRAN (R 3.2.1)
lazyeval 0.2.0 2016-06-12 CRAN (R 3.2.5)
lubridate * 1.6.0 2016-09-13 CRAN (R 3.2.5)
magrittr 1.5 2014-11-22 CRAN (R 3.2.1)
memoise 1.0.0 2016-01-29 CRAN (R 3.2.3)
munsell 0.4.3 2016-02-13 CRAN (R 3.2.5)
pipeR * 0.6.1.3 2016-04-04 CRAN (R 3.3.1)
plyr 1.8.4 2016-06-08 CRAN (R 3.2.5)
Rcpp 0.12.7 2016-09-05 CRAN (R 3.2.5)
readr * 1.0.0 2016-08-03 CRAN (R 3.2.5)
scales 0.4.1 2016-11-09 CRAN (R 3.3.2)
stringi 1.1.2 2016-10-01 CRAN (R 3.3.2)
stringr 1.1.0 2016-08-19 CRAN (R 3.2.5)
tibble 1.2 2016-08-26 CRAN (R 3.2.5)
withr 1.0.2 2016-06-20 CRAN (R 3.2.5)