R plotly 版本 4.5.2 散点图图例气泡大小设置 [英] R plotly version 4.5.2 scatterplot legend bubble size settings

查看:49
本文介绍了R plotly 版本 4.5.2 散点图图例气泡大小设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中使用 plotly 4.5.2.我创建了一个散点图,该散点图在变量上调整大小,问题是这些大小也反映在图例中,这使得它们难以阅读.

I am using plotly 4.5.2 in R. I have created a scatterplot which is sized on a variable, the issue is these sizes are also reflected in the legend which makes them hard to read.

我希望我的图表保持不变,唯一的例外是图例中气泡的大小.这些气泡既可以设置为相同大小,也可以缩放为更小的大小.重要的是,图表中的大小必须保持不变.

I want my graph to remain the same, with the only exception being the size of the bubbles in the legend. These bubbles can either be set to all be the same size or scaled to a smaller size. Importantly, the sizes in the graph must remain the same.

请在此处找到可重现的代码:

Please find reproducible code here:

library(plotly)

data <- data.frame(name = c('test1', 'test2', 'test3', 'test4'),
                      x = c(1, 15, 90, 45),
                      y = c(9, 43, 43, 53),
                      size = c(10000, 50000, 90000, 3000),
                      colour = c("rgba(230, 42, 56, 0.3)", "rgba(76, 175, 80, 0.3)",
                                 "rgba(32, 169, 242, 0.3)", "rgba(255, 193, 7, 0.3)")
                      )

plot <- plot_ly(data = data) %>% 
  add_trace(x = ~x,
            y = ~y,
            mode = 'markers',
            type = 'scatter',
            color = ~name,
            marker = list(
              color = ~colour,               
              opacity = 1,
              showlegend=T),
            size = ~size)

谢谢

推荐答案

我发现了一个 hack 来获得所需的输出,我在这里发布它是为了让其他人受益.

I found a hack to get the desired output, i'm posting it here for the benefit of others.

library(plotly)

data <- data.frame(name = c('test1', 'test2', 'test3', 'test4'),
                      x = c(1, 15, 90, 45),
                      y = c(9, 43, 43, 53),
                      size = c(10000, 50000, 90000, 3000),
                      colour = c("rgba(230, 42, 56, 0.3)", "rgba(76, 175, 80, 0.3)",
                                 "rgba(32, 169, 242, 0.3)", "rgba(255, 193, 7, 0.3)")
                      )


#Ranges
xmin <- - 0.2 * max(data[['x']])
xmax <- 1.8 * max(data[['x']])
ymin <- - 0.2 * max(data[['y']])
ymax <- 1.8 * max(data[['y']])


# Sum of the size variable
sum_size <- sum(data[['size']], na.rm = TRUE)

# Decimal size
data$size <- (data[['size']]/sum_size)

# Adjust for the smallest 
data <- data %>% mutate(size = ifelse(size < 0.05, 0.05, size))

#Size Vector
size <- data$size * 100

# not used atm
min_size <- min(data$size, na.rm = TRUE)
max_size <- max(data$size, na.rm = TRUE)


# Number of unique groups
num_bubbles <- length(unique(data[['name']])) 


# Artifical data used to resolve legend sizes
data2 <- data
data2$size <- min_size
data2[['x']] <- -2 * max(-xmin,-ymin)
data2[['y']] <- -2 * max(-xmin,-ymin)

# Bind the artifial data, plotly will only plot the original and this fixes the legend size issue
data <- rbind(data, data2)

plot <- plot_ly(data = data) %>% 
  add_trace(x = data[['x']],
            y = data[['y']],
            mode = 'markers',
            type = 'scatter',
            color = data[['name']], 
            marker = list(size = 10,
                          opacity = 1,sizemin=10,sizemax =100,sizeref = 100,
                          line = list(width = 2)),size = 30,showlegend=T,
            hoverinfo = "text") %>% 
  add_trace( x = -2 * max(-xmin,-ymin) , y = -2 * max(-xmin,-ymin), type = "scatter", mode = "markers", 
             color= data[['name']], showlegend=F) %>% config(modeBarButtonsToRemove = list("sendDataToCloud","pan2d","select2d","lasso2d","zoomIn2d","zoomOut2d","autoScale2d","resetScale2d","hoverClosestCartesian","hoverCompareCartesian"), displaylogo = FALSE, doubleClick = "reset")  



plot <- layout(plot,
               title = NULL,

               xaxis = list(           

                 title = 'x',
                 range = c(xmin,xmax),
                 showgrid = F     
               ),
               yaxis = list(         
                 title = 'y',

                 range = c(ymin,ymax)
               ))

plot <- plotly_build(plot)

for(i in seq(1,num_bubbles))
{
  plot$x$data[[i]]$marker$size <- c(size[i]*10000,min(size)*10000)


}

这篇关于R plotly 版本 4.5.2 散点图图例气泡大小设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆