【问题标题】:Bubble plot with bubbles representing absolute sizes带有代表绝对大小的气泡的气泡图
【发布时间】:2018-06-13 14:19:41
【问题描述】:

我正在尝试制作一个气泡图,其中每种颜色代表不同的珊瑚物种代码,气泡的大小代表以米为单位的个体大小,但也缩放到 x 或 y 轴,它们是也以米为单位。

我还想为我目前在 R 中使用 plotly 但愿意在 R 或 Python 中使用其他模块的个人的大小添加一个额外的图例。

我已经能够相对容易地按物种获得颜色,但我正在努力按大小缩放气泡。有没有人这样做过或者知道任何秘籍让它工作?

     #Example Data
     Species <- c('SSID','PAST','CNAT','SSID','MMEA','PAST')
     Dist <- c(7.1,4.0,6.4,8.0,8.1,8.9)
     XDist <- runif(6, 0.0, 1.0)
     Transect <- c(1,2,1,1,3,2)
     Width <- c(10,15,100,45,60,27)
     Data <- data.frame(Transect, Species, Dist, XDist, Width)
     XDist <- Data$Transect - 1
     Data$XDist <- Data$XDist + XDist

    library(plotly)

    k <- plot_ly(Data, x = ~XDist, y = ~Dist, type = 'scatter', mode = 
     'markers',
         size = ~Width ,marker = list(sizemode = 'diameter', opacity = 1,
         symbol = ifelse(data$Disease == 'Y', "circle-open", "circle"),
         line = list(width = 5)),
         color = ~Species, colors = 'Set1',
         hoverinfo = 'text',
         text = ~paste('Width:', Width, '<br>Species:', Species)
         ) %>%
     layout(title = 'Coral',
       xaxis = list(showgrid = FALSE),
       yaxis = list(showgrid = FALSE))

【问题讨论】:

  • 请提供示例数据。
  • 缩放点有什么问题?图片显示点被正确缩放。关于点大小的图例,这在之前的this previous question 中有介绍
  • 感谢第二个剧情的推荐。我认为这会奏效。至于点缩放,点相对于彼此适当缩放。我希望将这些点缩放到 X 或 y 轴。在示例数据中,CNAT = 100 cm 或 1 m 所以它在 XDist 中应该是“1”宽

标签: r ggplot2 plotly


【解决方案1】:

经过深思熟虑并找到了一对卡尺,我想我已经回答了自己的问题。假设宽度 = 1000,高度 = 1000,我的地块上的 100 厘米 = 3.72 毫米。 Plotly 似乎根据最大气泡和最小气泡的大小来缩放气泡。默认大小的气泡最大为 2.45mm,最小为 0.21mm。您可以使用这些测量值创建一个比率,以绝对根据距离缩放气泡。

    #Data
    Species <- c('SSID','PAST','CNAT','SSID','MMEA','PAST')
    Coral <- c(1,2,3,4,5,6)
    Dist <- c(1,2.4,4.6,3.2,1.2,4.1)
    XDist <- c(2,3,0.5,2.3,4.1,2.5)
    Transect <- c(1,2,1,1,3,2)
    Width <- c(10,15,100,45,60,27)
    Disease <- c(0,0,0,1,0,0)
    Data <- data.frame(Coral, Transect, Species, Dist, XDist, Width, Disease)        

    Datamax <- (((max(Data$Width, na.rm = T)*3.72)/100)/2.45)*100
    Datamin <- (((min(Data$Width, na.rm = T)*3.72)/100)/0.21)*10
    k <- plot_ly(Data, x = ~XDist, y = ~Dist, type = 'scatter',
         mode ='markers',
         size = ~Width ,marker = list(sizemode = 'diameter', opacity = 0.75,
         symbol = ifelse(Data$Disease == 1, "circle-open", "circle"),
         line = list(width = 10)),
         color = ~Species, colors = 'Set1',
         hoverinfo = 'text',
         text = ~paste('Width:', Width, '<br>Species:', Species),
         width = 1000,
         height = 1000,# ) %>%
         sizes = c(Datamin,Datamax)) %>%

    add_annotations(text = Data$Coral,        
         xanchor = 'center', showarrow = F) %>%
    layout(title = 'Coral',
         xaxis = list(showgrid = TRUE,
             range = c(-0.25,5.25),
             zeroline = T,
             showline = T,
             mirror = "ticks",
             gridcolor = toRGB("grey50"),
             gridwidth = 2),
         yaxis = list(showgrid = FALSE,
             scaleanchor = "x",
             rangemode = "nonnegative"))
    k
    export(k,file = "Test.CoralMap.png")

【讨论】:

    猜你喜欢
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多