簡體   English   中英

在R中繪制顏色和氣泡大小圖例

[英]Adding color and bubble size legend in R plotly

可能很簡單。

我有一個xy數據集,我想使用Rplotly 數據如下:

set.seed(1)
df <- data.frame(x=1:10,y=runif(10,1,10),group=c(rep("A",9),"B"),group.size=as.integer(runif(10,1,10)))

我想按df$group為數據着色,並使點的大小遵循df$group.size (即氣泡圖)。 另外,我想添加兩個圖例。

這是我的天真嘗試:

require(plotly)  
require(dplyr)  

    main.plot <-
 plot_ly(type='scatter',mode="markers",color=~df$group,x=~df$x,y=~df$y,size=~df$group.size,marker=list(sizeref=0.1,sizemode="area",opacity=0.5),data=df,showlegend=T) %>%
     layout(title="Title",xaxis=list(title="X",zeroline=F),yaxis=list(title="Y",zeroline=F))

結果如下: 在此處輸入圖片說明

不幸的是,至少把我想成為的傳說弄糟了:每個組的大小相同但顏色不同的點。

然后為group.size添加圖例,我遵循了this ,也得到了aocall的幫助:

legend.plot <- plot_ly() %>% add_markers(x = 1, y = unique(df$group.size),
                                size = unique(df$group.size),
                                showlegend = T, 
                                marker = list(sizeref=0.1,sizemode="area")) %>%
   layout(title="TITLE",xaxis = list(zeroline=F,showline=F,showticklabels=F,showgrid=F),
          yaxis=list(showgrid=F))

結果如下: 在此處輸入圖片說明

在這里,我的問題是,圖例包含了我的數據中不存在的值。

然后我用subplot組合它們:

subplot(legend.plot, main.plot, widths = c(0.1, 0.9))

我得到這個: 在此處輸入圖片說明

圖例標題被刪除的地方

因此,我將為您提供一些幫助。

首先,您只是將唯一值傳遞給圖例。 如果您傳遞所有可能的值(即seq(min(x), max(x), by=1)或本例中的seq_len(max(x)) ),圖例將顯示整個范圍。

其次, marker參數中的sizerefsizemode會更改點大小的計算方式。 以下示例應產生更一致的圖:

set.seed(1)
df <- data.frame(x=1:10,y=runif(10,1,10),group=c(rep("A",9),"B"),group.size=as.integer(runif(10,1,10)))

require(plotly)  
require(dplyr)  

a <- plot_ly(type='scatter',mode="markers",
    color=~df$group,
    x=~df$x,
    y=~df$y,
    size=df$group.size,
    marker = list(sizeref=0.1, sizemode="area"),
    data=df,
    showlegend=F) %>%
    layout(title="Title",
        xaxis=list(title="X",zeroline=F),
        yaxis=list(title="Y",zeroline=F))

b <- plot_ly() %>% add_markers(x = 1, y = seq_len(max(df$group.size)),
    size = seq_len(max(df$group.size)), 
    showlegend = F, 
    marker = list(sizeref=0.1, sizemode="area")) %>%
    layout(
        xaxis = list(zeroline=F,showline=F,showticklabels=F,showgrid=F),
        yaxis=list(showgrid=F))


subplot(b, a, widths = c(0.1, 0.9))

根據更新的請求:

請注意legend.plot的更改(將值映射為整數序列,然后手動更改軸刻度文本),並使用批注獲取圖例標題。 如此答案中所述 ,無論使用多少子圖,都只能使用一個標題。

圖例上的圓圈似乎對應於每條跡線的最小點大小。 因此,我在(12,12)處添加了一個點,並限制了軸的范圍以確保未顯示該點。

titleXtitleY控制軸標簽的顯示,如所解釋這里

set.seed(1)
df <- data.frame(x=1:10,y=runif(10,1,10),group=c(rep("A",9),"B"),group.size=as.integer(runif(10,1,10)))
require(plotly)  
require(dplyr)  


## Take unique values before adding dummy value
unique_vals <- unique(df$group.size)
df <- rbind(c(12, 12, "B", 1), df)
df[c(1, 2, 4)] <- lapply(df[c(1, 2, 4)], as.numeric)


main.plot <-
  plot_ly(type='scatter',
    mode="markers",
    color=~df$group,
    x=~df$x,
    y=~df$y,
    size=~df$group.size,
    marker=list(
        sizeref=0.1,
        sizemode="area",
        opacity=0.5),
        data=df,
        showlegend=T) %>%
  layout(title="Title",
    xaxis=list(title="X",zeroline=F, range=c(0, 11)),
    yaxis=list(title="Y",zeroline=F, range=c(0, 11)))

legend.plot <- plot_ly() %>% 
    add_markers(x = 1, 
    y = seq_len(length(unique_vals)),
    size = sort(unique_vals),
    showlegend = F, 
    marker = list(sizeref=0.1,sizemode="area")) %>%
    layout(
        annotations = list(
            list(x = 0.2, 
                y = 1, 
                text = "LEGEND TITLE", 
                showarrow = F, 
                xref='paper', 
                yref='paper')),
         xaxis = list(
            zeroline=F,
            showline=F,
            showticklabels=F,
            showgrid=F),
         yaxis=list(
            showgrid=F,
            tickmode = "array",
            tickvals = seq_len(length(unique_vals)),
            ticktext = sort(unique_vals)))


subplot(legend.plot, main.plot, widths = c(0.1, 0.9), 
    titleX=TRUE, titleY=TRUE)

暫無
暫無

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

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