簡體   English   中英

R plotly版本4.5.2散點圖傳奇氣泡大小設置

[英]R plotly version 4.5.2 scatterplot legend bubble size settings

我在R中使用了plotly 4.5.2。我創建了一個在變量上調整大小的散點圖,問題是這些大小也反映在圖例中,這使得它們難以閱讀。

我希望我的圖形保持不變,唯一的例外是圖例中氣泡的大小。 這些氣泡可以設置為全部相同的尺寸或縮放到更小的尺寸。 重要的是,圖表中的大小必須保持不變。

在此輸入圖像描述

請在此處找到可重現的代碼:

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)

謝謝

我發現了一個黑客來獲得所需的輸出,我發布它是為了其他人的利益。

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)


}

在此輸入圖像描述

不確定如何將其合並到劇情本身,但如果您在Shiny應用中遇到此類問題,請嘗試在css文件中使用此功能或在UI中使用inlineCSS:

.legendpoints path.scatterpts {
  d: path('M 7.5 0 A 7.5 7.5 0 1 1 0 -7.5 A 7.5 7.5 0 0 1 7.5 0 Z');
}

將每個'7.5'替換為圖例條目的px大小的一半(所以這里將是15px)

它在應用程序中的情節看起來如何

你可以使用ggplotggplotly()函數:

library(ggplot2)
p <- ggplot(data, aes(x=x, y=y, size=size, color=name)) + 
           geom_point() + 
           theme_minimal() + 
           theme(legend.title=element_blank())
ggplotly(p)

在此輸入圖像描述

但是plotly_4.5.2和圖例功能存在同樣的問題。 從圖例中刪除大小的兩種方法都在ggplot2_2.1.0中工作,但不能使用轉換函數ggplotly()

p <- p + guides(size=FALSE)
p <- p + scale_size_continuous(guide=FALSE)
ggplotly(p)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM