简体   繁体   中英

R Plotly scatter ternary colorbar

I am using Plotly to make a scatter ternary plot. I want to color points by one of the values in the data frame (titled 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.
此代码输出的图片

It looks like you were missing showscale=TRUE in the trace definition.
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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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