简体   繁体   English

R Plotly 散射三元彩条

[英]R Plotly scatter ternary colorbar

I am using Plotly to make a scatter ternary plot.我正在使用 Plotly 来制作散射三元 plot。 I want to color points by one of the values in the data frame (titled mu).我想通过数据框中的值之一(标题为 mu)为点着色。 However, the colorbar isn't showing.但是,颜色条没有显示。 Here is my code:这是我的代码:

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

Somehow the colorbar is still not showing.不知何故,彩条仍未显示。 I'm not sure why because all the Plotly scatterplot examples online make getting the colorbar to show up seem easy.我不知道为什么,因为所有 Plotly 散点图示例在线使颜色条显示看起来很容易。
此代码输出的图片

It looks like you were missing showscale=TRUE in the trace definition.看起来您在跟踪定义中缺少showscale=TRUE
Trying:试:

#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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM