Add a size gradient legend in a world map using plotly

Hi! I am using Plotly in R. I want to create a world map and have two legends that will show distinct values. The first is the colour which corresponds to the number of papers published in each country. The second corresponds to the number of rootstocks used in each paper. These dots get bigger as there are more rootstocks used in the paper. Therefore, I would like to display a legend that shows what size fits the number of rootstocks. This legend should not consider a color gradient

However, it seems like the colour bar doesn't allow me to add values. When I remove this feature, the rootstocks legend works. I tried many different codes, also trying to do it with ggplot, but didn't succeed.

Here's my code, which will be followed by my data sets.

fig_jittered <- plot_geo(occurence) %>%
  layout(
    geo = list(
      showframe = TRUE,
      showcoastlines = TRUE,
      showland = TRUE,
      landcolor = toRGB("white"),
      countrycolor = toRGB("darkgrey"),
      coastlinecolor = toRGB("black"),
      coastlinewidth = 0.5,
      lataxis = list(
        range = c(-55, 80),
        showgrid = FALSE
      ),
      lonaxis = list(
        range = c(-130, 160),
        showgrid = FALSE
      )
    )
  ) %>%
  add_trace(
    z = ~count, color = ~count, colors = 'GnBu',
    text = ~COUNTRY, locations = ~code,
    marker = list(
      line = list(width = 0.5, color = "black")
    ),
    colorbar = list(title = "Number of papers published in each country", x = 1, y = 0.92)
  ) %>%
  add_trace(
    type = "scattergeo",
    lat = ~duse_no_dupplicats$lat_jittered, 
    lon = ~duse_no_dupplicats$lon_jittered,
    text = ~paste("Title: ", duse_no_dupplicats$title, "<br>Number of Rootstocks: ", duse_no_dupplicats$n_rootstock),
    mode = "markers",
    marker = list(
      size = duse_no_dupplicats$dotsize_rootstock,
      symbol = "circle",
      color = "lightgrey",
      line = list(width = 1.5, color = "black")
    ),
    name = "Number of Rootstocks" # Add the legend label
  ) %>% 
  layout(
    title = "With jittered locations",
    legend = list( # Customize the legend position and appearance
      x = 0.05,
      y = 0.95,
      bgcolor = "rgba(255, 255, 255, 0.7)",
      bordercolor = "black",
      borderwidth = 1
    )
  )

fig_jittered

duse_no_dupplicats

dput(dusecut)
structure(list(id = c("1", "5", "10", "20", "21", "24", "25",
"47", "58", "65", "68", "76", "83", "91", "103", "114", "116",
"124", "127", "134", "135", "140", "141", "142", "145", "146",
"174", "176", "180", "184", "188", "195", "208", "214", "216",
"219", "222", "233", "249", "268"), lat_jittered = c(38.7377064359404,
43.5761112164898, 37.9235031866915, 46.3098119040726, 36.5433768246047,
33.7332840405519, 43.5035202752525, 44.7743270278773, 44.2681201993407,
32.7704741151927, 34.0638900351183, 29.4918581168696, 48.3634134447124,
46.9264395307103, 44.9000974477011, 50.0297460009899, -33.9590650219929,
40.0440757318027, 47.8230118127686, 38.5980070016817, 37.1718596254961,
-23.126341973152, 39.3257486161124, -9.22739536766893, -9.06022982656411,
-20.3531972613832, 38.4468812217009, 38.7332140110433, 41.4678715980339,
-34.1984225050714, -29.2180914001753, 44.0357768001092, 47.6772796015891,
-34.3220146662689, 30.8555836620263, 42.8991637902538, 43.9426254258141,
50.0253479496291, 43.9733679219306, 38.2798062185569), lon_jittered = c(-8.32537908562589,
12.0947568336135, -121.691289428227, -119.075707938975, -118.580631370821,
73.3190522662625, 12.1431015449209, -0.146355702619443, 86.4102098408073,
51.5930024228714, -116.740621781656, 80.6078221716285, 16.6181140353805,
16.6316830731611, 0.251698475820558, 7.22708447587718, 18.0598971270631,
116.675036072787, 16.7021728935854, -120.951964736247, -94.7868793825128,
-46.3831338794722, 118.427028340939, -39.6900623886666, -39.8427074421888,
-49.7928484643629, -122.005469751341, -7.52678108194903, 15.8199758732707,
141.563458767373, -51.1325258902965, 4.46389363309673, 17.2382388971558,
142.851770006896, 33.9525991678717, -85.1173569477579, 87.8841016072193,
7.24152549942959, 4.48478443562873, -121.569625517062), n_rootstock = c(NA,
2, 15, 5, NA, 5, 2, 2, 5, 2, 3, 4, 3, 5, 3, 2, 2, 5, 1, 2, 4,
2, 7, 2, 6, 2, 8, 1, 5, 7, 15, 8, NA, 5, 2, 2, 3, 2, 1, 14),
dotsize_rootstock = c(NA, 1.5, 11.25, 3.75, NA, 3.75, 1.5,
1.5, 3.75, 1.5, 2.25, 3, 2.25, 3.75, 2.25, 1.5, 1.5, 3.75,
0.75, 1.5, 3, 1.5, 5.25, 1.5, 4.5, 1.5, 6, 0.75, 3.75, 5.25,
11.25, 6, NA, 3.75, 1.5, 1.5, 2.25, 1.5, 0.75, 10.5)), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 30L, 31L,
33L, 34L, 36L, 37L, 39L, 40L, 41L, 42L, 43L, 46L, 49L, 52L), class = "data.frame")

Occurence

dput(occurence)
structure(list(COUNTRY = c("Australia", "Austria", "Brazil",
"China", "Czech Republic", "Egypt", "France", "Germany", "Greece",
"Hungary", "India", "Iran", "Israel", "Italy", "Japan", "Pakistan",
"Portugal", "South Africa", "Spain", "Turquie", "United States"
), code = c("AUS", "AUT", "BRA", "CHN", "CZE", "EGY", "FRA",
"DEU", "GRC", "HUN", "IND", "IRN", "ISR", "ITA", "JPN", "PAK",
"PRT", "ZAF", "ESP", "TUR", "USA"), count = c(5L, 3L, 6L, 4L,
1L, 2L, 8L, 4L, 2L, 3L, 1L, 1L, 2L, 9L, 1L, 1L, 7L, 9L, 3L, 1L,
18L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-21L))

If someone could help me it would be great as I am a little a little lost.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.