廢話不多說,直接附上code
影片含有程式碼詳細解說,若有誤再煩請告知,謝謝
library(ggplot2)
library(dplyr)
data <- data.frame(
day = as.Date("2019-01-01") + 0:99,
temperature = runif(100) + seq(1,100)^2.5 / 10000,
energy = runif(100) + seq(100,1)^1.5 / 10
)
##雙軸####
data %>%
ggplot(aes(x=day)) +
geom_line(aes(y=energy), size = 1.5, color="red", group = 1) +
geom_line(aes(y=temperature*10), size = 1.5, color="blue", group = 1) +
scale_y_continuous(name = "energy",
sec.axis = sec_axis(~./10, name="temperature"))