[英]R plot. Celsius on y axis with exact Fahrenheit conversion of the y labels on second y axis
我在y軸上有一個攝氏溫度的圖:
plot(y=0:100,x=0:100, main="temperature",xlab="time",ylab="Celsius",type="l")
如何繪制相同的次級Y刻度,但在y軸上以攝氏度顯示為單位,在第二y軸上以華氏度顯示。 T(°F)= T(°C)×9/5 + 32我需要兩個y軸的標簽位置完全對應,以便次級y標簽顯示位於主y標簽上的轉換值。
謝謝您的幫助。
在最粗略的形式中,您可以使用axis()
:
plot(y=0:100,x=0:100, main="temperature",xlab="time",ylab="Centigrate",type="l")
axis(4, at=0:100, labels=0:100 * 9/5 + 32)
通過使用seq(0, 100, by=10)
可以獲得更少的標簽。 您還需要設置par(mar=)
以適合您的軸標簽。
這是一個左右兩側都帶有漂亮刻度的示例。
set.seed(1)
temp <- rnorm(10) * 10 + 10 # random temperatures
par(mar=c(5.1, 5.1, 5.1, 5.1)) # extra margin space
plot(temp, ylab="degrees C")
ylim <- par("yaxp")[1:2] # get range of original y-axis
# slope and offset coefficients to convert between degrees C and F
slope <- 9/5
offset <- 32
alt.ax <- pretty(slope * ylim + offset)
alt.at <- (alt.ax - offset) / slope
axis(side=4, at=alt.at, labels=alt.ax, srt=90)
mtext("degrees F", side=4, line=par("mgp")[1])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.