2015-08-19 103 views
0

在y标签我有在Y轴上摄氏的曲线的确切华氏转换Y轴摄氏:R plot。上与第二Y轴

plot(y=0:100,x=0:100, main="temperature",xlab="time",ylab="Celsius",type="l") 

如何可以绘制的相同的二级Y标尺,但与的单位,在y轴上以摄氏度显示为第二y轴上的华氏温度。 T(°F)= T(℃)×9/5 + 32 我需要两个y轴的标记的位置,以准确地对应,以使辅助y标签显示转换后的值,是主y标签上。

谢谢你的帮助。

+1

注:我们不使用摄氏度了,但摄氏度。 –

+0

的http://stackoverflow.com/questions/21375505/r-creating-graphs-with-two-y-axes – maj

+4

可能重复的可能的复制[I如何与2个不同的y轴绘制?](HTTP:/ /stackoverflow.com/questions/6142944/how-can-i-plot-with-2-different-y-axes) –

回答

2

在这恶劣的形式,你可以使用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=)以符合您的轴标签。

1

下面是一个例子与左右两侧漂亮的蜱。

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]) 

output