如何优化Y轴标题和数据表示的位置?

问题描述 投票:0回答:1

我怎样才能在情节中: a) 将轴标题水平放置在各自的轴上方? b) 减小 y=0 和 X 轴之间的间距? c) 将 Power 在增加之前和减少之后(y=0 处)的绿色数据点绘制得与 y>0 处的绿色数据点一样粗?

非常感谢您的支持。

library(plotly)

# Sample data
d <- data.frame(
  time = seq(1, 600, by = 1),
  VO2 = c(rnorm(120, 250, 10), rnorm(120, 300, 15), seq(300, 2000, length.out = 240), rnorm(120, 300, 15)),
  VCO2 = c(rnorm(120, 200, 10), rnorm(120, 250, 15), seq(250, 1800, length.out = 240), rnorm(120, 250, 15)),
  power = c(rep(0, 120), rep(0, 120), seq(0, 160, length.out = 240), rep(0, 120))
)

# Create the plot
fig <- plot_ly(data = d) %>% 
  add_lines(x = ~time, y = ~VO2, 
            line = list(color = 'blue'), name = "VO2") %>% 
  add_lines(x = ~time, y = ~VCO2, yaxis = "y2", 
            line = list(color = 'red'), name = "VCO2") %>%
  add_lines(x = ~time, y = ~power, yaxis = "y3", 
            line = list(color = 'green'), name = "Power") %>%
  layout(
    xaxis = list(
      title = list(text = 'Time [sec]', standoff = 10),
      color = 'black', 
      showline = TRUE, 
      tickcolor = 'black', 
      tickwidth = 2, 
      linewidth = 2,
      ticks = "outside"
    ),
    yaxis = list(
      title = list(text = 'VO2', standoff = 20), 
      showline = TRUE, 
      side = "left", 
      color = 'blue', 
      tickcolor = 'blue', 
      tickwidth = 2, 
      linewidth = 2,
      ticks = "outside",
      range = c(0, max(c(d$VO2, d$VCO2)))
    ),
    yaxis2 = list(
      title = list(text = 'VCO2', standoff = 20), 
      showline = TRUE, 
      overlaying = "y", 
      anchor = "free", 
      side = "left", 
      color = 'red', 
      tickcolor = 'red', 
      tickwidth = 2, 
      linewidth = 2,
      ticks = "outside",
      position = -0.1,
      range = c(0, max(c(d$VO2, d$VCO2)))
    ),
    yaxis3 = list(
      title = list(text = 'Power', standoff = 20), 
      showline = TRUE, 
      overlaying = "y", 
      side = "right", 
      color = 'green', 
      tickcolor = 'green', 
      tickwidth = 2, 
      linewidth = 2,
      ticks = "outside",
      range = c(0, max(d$power) * 1.25)
    ),
    showlegend = FALSE, # Removes legend
    margin = list(pad = 50, b = 0, l = 100, r = 100)
  ) 

fig
r plotly yaxis x-axis
1个回答
0
投票

尝试

matplot
,将幂列乘以 10。

clr <- c('blue', 'red', 'darkgreen')
par(mar=c(4, 4.5, 2, 4.5))
matplot(t(t(d[-1])*c(1, 1, 10)), type='l', axes=FALSE, ann=FALSE, lty=1, col=clr)
box()
axis(1); axis(2, las=1); axis(4, axTicks(2), labels=axTicks(2)/10, col.axis=clr[3], col=clr[3], las=1)
mtext('Time [sec]', 1, 2.5); mtext('Volume', 2, 3); mtext('Power', 4, 3, col=clr[3])
legend('topleft', lty=1, col=clr, leg=c('VO2', 'VCO2', 'Power'), bty='n')

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.