我有一个绘图,其中数据框中的所有其他列在y轴的左侧和右侧都有y轴标签。如果不使用rplotly,我可以使用该图,但是现在我想在rplotly中使用它。
下面是我尝试过的代码,plotly之前的部分,这就是我希望plotly图看起来的方式。
library(plotly)
#this part works
x <- 1:10
# Generate 4 different sets of outputs
y1 <- runif(10, 0, 1)
y2 <- runif(10, 100, 150)
y3 <- runif(10, 1000, 2000)
y4 <- runif(10, 40000, 50000)
y5 <- runif(10, 0, 1)
y6 <- runif(10, 100, 200)
y <- data.frame(y1, y2, y3, y4, y5, y6)
# Colors for y[[2]], y[[3]], y[[4]] points and axes
colors = c("red", "blue", "green","orange","black","purple")
count <- length(names(y))
# Set the margins of the plot wider
par(oma = c(0, 2, 2, 3))
plot(x, y[[1]], yaxt = "n",ylab = "",type="l")
lines(x, y[[1]],col = colors[1])
# We use the "pretty" function go generate nice axes
axis(at = pretty(y[[1]]), side = 2, col = colors[1])
# The side for the axes so right left right left...
sides <- list(4, 2, 4, 2, 4)
# The number of "lines" into the margin the axes will be
lines <- list(NA, 2, 2, 4, 4)
for(i in 2:count) {
par(new = TRUE)
plot(x, y[[i]], axes = FALSE, col = colors[i], xlab = "", ylab = "",type="l")
axis(at = pretty(y[[i]]), side = sides[[i-1]], line = lines[[i-1]],
col = colors[i])
lines(x, y[[i]], col = colors[i])
legend("topright",legend = names(y),fill=colors)
}
###############################################################
#this is what I tried to get same outcome in rplotly
p <- plot_ly(y, y=y[[1]], x=x , type="scatter", mode="lines",color=colors[1],name=names(y[1]))
for(i in 2:count) {
p<-add_trace(p, y=y[[i]], x=x , type="scatter", mode="lines",color=colors[i],name=names(y[i]))
}
p
答案 0 :(得分:0)
现在我得到了部分解决方案,但是我仍然需要y5,y6轴,因为它们位于其他y轴的顶部。我认为我必须更改,position =“”部分,但我还没有弄清楚要使用的正确数字。
这是下面的更新代码
library(plotly)
x <- 1:10
# Generate 4 different sets of outputs
y1 <- runif(10, 0, 1)
y2 <- runif(10, 100, 150)
y3 <- runif(10, 1000, 2000)
y4 <- runif(10, 40000, 50000)
y5 <- runif(10, 0, 1)
y6 <- runif(10, 100, 200)
y <- data.frame(y1, y2, y3, y4, y5, y6)
plot_ly(data = y, x = x, y = y$y1
,type = "scatter", mode = "lines", width = 1000, color = I("red")
,name = "name01") %>%
add_trace(x = x, y = y$y2, yaxis = "y2", color = I("blue"), name = "name02") %>%
add_trace(x = x, y = y$y3, yaxis = "y3", color = I("purple"), name = "name03") %>%
add_trace(x = x, y = y$y4, yaxis = "y4", color = I("green"), name = "name04") %>%
add_trace(x = x, y = y$y5, yaxis = "y5", color = I("orange"), name = "name05") %>%
add_trace(x = x, y = y$y6, yaxis = "y6", color = I("yellow"), name = "name06") %>%
layout(
yaxis = list(
showline = TRUE
,side = "left"
,anchor = "free"
,position = NA
,overlaying = NA
,color = "red"
,ticks = "outside"
,showgrid = FALSE
)
,yaxis2 = list(
showline = TRUE
,side = "right"
,anchor = "free"
,position = 1
,overlaying = "y"
,color = "blue"
,ticks = "outside"
,showgrid = FALSE
)
,yaxis3 = list(
showline = TRUE
,side = "left"
,anchor = NA
,position = NA
,overlaying = "y"
,color = "purple"
,ticks = "outside"
,showgrid = FALSE
)
,yaxis4 = list(
showline = TRUE
,side = "right"
,anchor = NA
,position = NA
,overlaying = "y"
,color = "green"
,ticks = "outside"
,showgrid = FALSE
)
,yaxis5 = list(
showline = TRUE
,side = "left"
,anchor = NA
,position = NA
,overlaying = "y"
,color = "orange"
,ticks = "outside"
,showgrid = FALSE
)
,yaxis6 = list(
showline = TRUE
,side = "right"
,anchor = NA
,position = NA
,overlaying = "y"
,color = "yellow"
,ticks = "outside"
,showgrid = FALSE
)
,showlegend = TRUE
,margin = list(
pad = 35, b = 60, l = 90, r = 200
)
,legend = list(x=0,y=0,orientation = "h")
)