![](/img/trans.png)
[英]Line Plot in R with two y-axes using twoord.plot: Values of first y-axis missing
[英]Creating a line plot using plot_ly (R) with two y-axes
我是plot_ly軟件包的新手,正在嘗試生成一個在y軸上具有兩個變量的時間序列線圖。
在數據框“ baro”中,我有POSIXct格式的“ DateTime”變量,以及數字格式的“ Pressure”和“ Temperature”。
我的代碼基於此處給出的示例: https : //plot.ly/r/multiple-axes/
p <- plot_ly(baro)
add_trace(p, x = ~DateTime, y = ~Pressure, type = "scatter",
mode = "lines", name = "Pressure")
add_trace(p, x = ~DateTime, y = ~Temperature, type = "scatter",
mode = "lines", name = "Temperature", yaxis = "y2")
layout(p,
title = "Pressure & Temperature", yaxis2 = ay,
xaxis = list(title="x")
)
這將輸出一組在x軸上標記為-1至6並在y軸上標記為-1至4的軸,而沒有繪制數據。
我更喜歡使用管道%>%
而不是將對象歸因於繪圖。 當您有2個Y軸時,最好明確設置每個Y軸的布局。
這應該做您想要的:
# Build randon data
set.seed(123)
baro = data.frame(DateTime = as.POSIXct(1:10,origin = "2019-01-01"),
Pressure = sample(1000:2000,10),
Temperature = sample(20:60,10)
)
# Build plot
baro %>%
plot_ly(type = "scatter", mode = "lines") %>%
add_trace(x = ~DateTime, y = ~Pressure, name = "Pressure")%>%
add_trace(x = ~DateTime, y = ~Temperature, name = "Temperature", yaxis = "y2") %>%
layout(title = "Pressure & Temperature",
yaxis = list(title = "Pressure"),
yaxis2 = list(title = "Temperature",
overlaying = "y",
side = "right"
)
)
這里的輸出:
最好的祝福。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.