Home > Enterprise >  How to hide, or at least customize colorbar in plotly scattergeo in R
How to hide, or at least customize colorbar in plotly scattergeo in R

Time:05-14

I want to create a scattergeo plot with markers for capitals. These markers are sized and colored regarding database values.

If I use the standard colors, everything goes well : the map is shown properly with good size, different colors, and no legend (I add the information country:value in a hover text on the markers).

However if I use a custom palette using colors feature into my scattergeo plot the colorbar is always displayed. Showlegend=F and Showscale=F doesn't help. Since I remove colors the colorbar disapeared. Moreover, if I try to customize it (eg change the title or the format of the tickslabels) it doesn't work.

In other words no option I have tried on this colorbar works !

This is my data :

structure(list(ISO3 = c("ARG", "AUS", "AUT", "BEL", "BRA", "CAN", 
"CHE", "CHL", "CHN", "COK", "DEU", "DNK", "ESP", "FIN", "FJI", 
"FRA", "GBR", "HKG", "IDN", "IND", "ITA", "JPN", "KOR", "LUX", 
"MEX", "MYS", "NCL", "NLD", "NOR", "NZL", "PHL", "PRT", "RUS", 
"SGP", "SWE", "THA", "TON", "USA", "WSM"), Total = c(1073L, 8204L, 
818L, 1502L, 1871L, 7958L, 3524L, 2456L, 3345L, 456L, 5010L, 
569L, 2775L, 184L, 75L, 60382L, 4424L, 415L, 146L, 405L, 8369L, 
8176L, 1034L, 235L, 961L, 137L, 6522L, 667L, 309L, 7960L, 238L, 
316L, 486L, 404L, 480L, 200L, 41L, 85225L, 46L), Size = c(16, 
30, 14, 18, 19, 30, 24, 21, 23, 12, 26, 13, 22, 8, 5, 50, 25, 
11, 7, 11, 30, 30, 16, 9, 15, 7, 28, 13, 10, 30, 9, 10, 12, 11, 
12, 8, 3, 54, 4), Color = c(3, 4, 3, 3, 3, 4, 4, 3, 4, 3, 4, 
3, 3, 2, 2, 5, 4, 3, 2, 3, 4, 4, 3, 2, 3, 2, 4, 3, 2, 4, 2, 2, 
3, 3, 3, 2, 2, 5, 2), ISO2 = c("AR", "AU", "AT", "BE", "BR", 
"CA", "CH", "CL", "CN", "CK", "DE", "DK", "ES", "FI", "FJ", "FR", 
"GB", "HK", "ID", "IN", "IT", "JP", "KR", "LU", "MX", "MY", "NC", 
"NL", "NO", "NZ", "PH", "PT", "RU", "SG", "SE", "TH", "TO", "US", 
"WS"), LABELFR = c("Argentine", "Australie", "Autriche", "Belgique", 
"Brésil", "Canada", "Suisse", "Chili", "Chine", "Iles Cook", 
"Allemagne", "Danemark", "Espagne", "Finlande", "Fidji", "France", 
"Royaume-Uni", "Hong-kong, Chine", "Indonésie", "Inde", "Italie", 
"Japon", "Corée, République de", "Luxembourg", "Mexique", "Malaisie", 
"Nouvelle-Calédonie", "Pays-Bas", "Norvège", "Nouvelle-Zélande", 
"Philippines", "Portugal", "Russie, Fédération de", "Singapour", 
"Suède", "Thaïlande", "Tonga", "Etats-Unis", "Samoa"), LABELEN = c("Argentina", 
"Australia", "Austria", "Belgium", "Brazil", "Canada", "Switzerland", 
"Chile", "China", "Cook Islands", "Germany", "Denmark", "Spain", 
"Finland", "Fiji", "France", "United Kingdom", "Hong Kong", "Indonesia", 
"India", "Italy", "Japan", "South Korea", "Luxembourg", "Mexico", 
"Malaysia", "New Caledonia", "Netherlands", "Norway", "New Zealand", 
"Philippines", "Portugal", "Russia", "Singapore", "Sweden", "Thailand", 
"Tonga", "United States", "Samoa"), CAPITAL = c("Buenos Aires", 
"Canberra", "Vienna", "Brussels", "Brasilia", "Ottawa", "Bern", 
"Santiago", "Beijing", "Avarua", "Berlin", "Copenhagen", "Madrid", 
"Helsinki", "Suva", "Paris", "London", "N/A", "Jakarta", "New Delhi", 
"Rome", "Tokyo", "Seoul", "Luxembourg", "Mexico City", "Kuala Lumpur", 
"Noumea", "Amsterdam", "Oslo", "Wellington", "Manila", "Lisbon", 
"Moscow", "Singapore", "Stockholm", "Bangkok", "Nuku'alofa", 
"Washington", "Apia"), LATITUDE = c("-34.583333333333336", "-35.266666666666666", 
"48.2", "50.833333333333336", "-15.783333333333333", "45.416666666666664", 
"46.916666666666664", "-33.45", "39.916666666666664", "-21.2", 
"52.516666666666666", "55.666666666666664", "40.4", "60.166666666666664", 
"-18.133333333333333", "48.86666666666667", "51.5", "0", "-6.166666666666667", 
"28.6", "41.9", "35.68333333333333", "37.55", "49.6", "19.433333333333334", 
"3.1666666666666665", "-22.266666666666666", "52.35", "59.916666666666664", 
"-41.3", "14.6", "38.71666666666667", "55.75", "1.2833333333333332", 
"59.333333333333336", "13.75", "-21.133333333333333", "38.883333", 
"-13.816666666666666"), LONGITUDE = c("-58.666667", "149.133333", 
"16.366667", "4.333333", "-47.916667", "-75.700000", "7.466667", 
"-70.666667", "116.383333", "-159.766667", "13.400000", "12.583333", 
"-3.683333", "24.933333", "178.416667", "2.333333", "-0.083333", 
"0.000000", "106.816667", "77.200000", "12.483333", "139.750000", 
"126.983333", "6.116667", "-99.133333", "101.700000", "166.450000", 
"4.916667", "10.750000", "174.783333", "120.966667", "-9.133333", 
"37.600000", "103.850000", "18.050000", "100.516667", "-175.200000", 
"-77.000000", "-171.766667"), CONTINENT = c("South America", 
"Australia", "Europe", "Europe", "South America", "Central America", 
"Europe", "South America", "Asia", "Australia", "Europe", "Europe", 
"Europe", "Europe", "Australia", "Europe", "Europe", "Asia", 
"Asia", "Asia", "Europe", "Asia", "Asia", "Europe", "Central America", 
"Asia", "Australia", "Europe", "Europe", "Australia", "Asia", 
"Europe", "Europe", "Asia", "Europe", "Asia", "Australia", "Central America", 
"Australia")), class = c("data.table", "data.frame"), row.names = c(NA, 
-39L), .internal.selfref = <pointer: 0x000001ffb6417970>, sorted = "ISO3")

This is my code :

fig <- plot_ly(
  type = 'scattergeo',
  showlegend=F,
  mode='markers',
  data=TOUR,
  y=~LATITUDE,
  x=~LONGITUDE,
  text=sprintf("%s : %s",TOUR$LABELFR,TOUR$Total),
  hovertemplate = "%{text}<extra></extra>",
  colors=c(ispfPalette[c(9,4,2)]),
  color=~Color,
  marker=list(
    showscale=F,
    size=~Size,
    reversescale=F
    )
  )

And this is the output I have :

enter image description here

Best solution would be to hide completely the colorbar, but I would also be curious how to customize it by changing the title and formatting the values (per example in case of % or if I want to change the decimal separator).

Thanks for your help !

CodePudding user response:

Just add: %>% hide_colorbar at the end of your code:

fig <- plot_ly(
  type = 'scattergeo',
  showlegend=F,
  mode='markers',
  data=TOUR,
  y=~LATITUDE,
  x=~LONGITUDE,
  text=sprintf("%s : %s",TOUR$LABELFR,TOUR$Total),
  hovertemplate = "%{text}<extra></extra>",
  colors=c(ispfPalette[c(9,4,2)]),
  color=~Color,
  marker=list(
    showscale=F,
    size=~Size,
    reversescale=F
    )
  ) %>% hide_colorbar()

enter image description here

  • Related