pplot2做雙y軸圖
創(chuàng)建示例數(shù)據(jù)
我們將使用以下數(shù)據(jù)作為本 R 教程的基礎(chǔ)。
set.seed(93756) ? ? ? ? ? ? ? ? ?# Create example data
x <- rnorm(50)
y <- x + 0.3 * rnorm(50)
data <- data.frame(x, y)
關(guān)鍵函數(shù)是 sec_axis,這個是雙坐標(biāo)系:
ggplot(data, aes(x, y)) + ? ? ? ?# Create ggplot2 plot?
?geom_point()+ ?scale_y_continuous( ? ?"Kilometers", ? ?sec.axis = sec_axis(~ . * 1000, name = "Meters") ?)

使用雙Y軸同時顯示不同范圍的數(shù)值系列:
coeff <- 10? # 將數(shù)據(jù)縮放到同樣的范圍
ggplot(data, aes(x=day)) + geom_line( aes(y=temperature)) + ??geom_line( aes(y=price /coeff)) +?scale_y_continuous(name =?"First Axis",
? ? ? ? ? ? ? sec.axis =?sec_axis(~.*coeff,?name="Second Axis") ?)??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 在軸顯示是尺寸要恢復(fù)? ??? ? ?

?
標(biāo)簽: