R散点图三元色条

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

我正在使用Plotly制作散点三元图。我想用数据框中的值之一(标题为mu)为点着色。但是,颜色栏未显示。这是我的代码:

library(plotly)

df <- eqData0

# axis layout
axis <- function(title) {
  list(
    title = title,
    titlefont = list(
      size = 20
    ),
    tickfont = list(
      size = 15
    ),
    tickcolor = 'rgba(0,0,0,0)',
    ticklen = 5
  )
}


fig <- df %>% plot_ly()
fig <- fig %>% add_trace(
    type = 'scatterternary',
    mode = 'markers',
    a = ~u1eq,
    b = ~u2eq,
    c = ~bueq,
    marker = list( 
      symbol = 100,
      color = ~mu,
      size = 14,
      line = list('width' = 2),
      colorscale = 'YlGnBu'
    ),
    colorbar = list(
      xanchor = 'middle',
      yanchor = 'middle'
    )
)
m <- list(
  l = 50,
  r = 50,
  b = 100,
  t = 100,
  pad = 4
)
fig <- fig %>% layout(autosize = F, width = 500, height = 500, margin = m,
    ternary = list(
      sum = 1,
      aaxis = axis(TeX("$u_1$")),
      baxis = axis(TeX("$u_2$")),
      caxis = axis(TeX("$\\bar{u}$"))
    )

  )
fig <- fig %>% config(mathjax = 'cdn')
fig

以某种方式仍然无法显示颜色栏!我不确定为什么会这样,因为所有在线的Plotly散点图示例都使显示颜色条看起来很容易。“此代码输出的图片”

r plotly colorbar scatter ternary
1个回答
0
投票

似乎您在跟踪定义中缺少showscale=TRUE。尝试:

#fake data:
df <- data.frame(u1eq = c(0.2, 0.3, 0.5), u2eq=c(0.6, 0.3, 0.1), bueq=c(0.2, 0.4, 0.4), mu=c(1, 1.8, 2))

# axis layout
axis <- function(title) {
   list(
      title = title,
      titlefont = list(
         size = 20
      ),
      tickfont = list(
         size = 15
      ),
      tickcolor = 'rgba(0,0,0,0)',
      ticklen = 5
   )
}

fig <- df %>% plot_ly()
fig <- fig %>% add_trace(
   type = 'scatterternary',
   mode = 'markers',
   a = ~u1eq,
   b = ~u2eq,
   c = ~bueq,
   marker = list( 
      colorscale = 'YlGnBu',
      symbol = 100,
      color = ~mu,
      size = 14,
      line = list('width' = 2),
      showscale   = TRUE
   )
)
m <- list( l = 50,  r = 50,  b = 100,  t = 100,  pad = 4) 

fig <- fig %>% layout(autosize = F, width = 500, height = 500, margin = m,
                      ternary = list(
                         sum = 1,
                         aaxis = axis(TeX("$u_1$")),
                         baxis = axis(TeX("$u_2$")),
                         caxis = axis(TeX("$\\bar{u}$"))  )

)  %>% config(mathjax = 'cdn')
fig

enter image description here

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