如何在 R 中的 plotly scattergeo 中隐藏或至少自定义颜色条

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

我想创建一个带有首都标记的散点图。这些标记的大小和颜色与数据库值有关。

如果我使用标准颜色,一切顺利:地图显示正确,大小合适,颜色不同,没有图例(我在标记上的悬停文本中添加信息 country:value)。

但是,如果我在我的散点图中使用使用颜色功能的自定义调色板,则始终显示颜色栏。 Showlegend=F 和 Showscale=F 没有帮助。由于我删除了颜色,颜色条消失了。 此外,如果我尝试自定义它(例如更改标题或标记标签的格式),它也不起作用。

换句话说,我在这个颜色条上尝试过的选项都没有用!

这是我的数据:

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")

这是我的代码:

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
    )
  )

这是我的输出:

最好的解决方案是完全隐藏颜色栏,但我也很好奇如何通过更改标题和格式化值来自定义它(每个示例在 % 的情况下或者如果我想更改小数点分隔符) .

感谢您的帮助!

根据要求更新

您可以修改 colors 参数中的颜色栏,例如:

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="YlOrRd",
 #colors = c("#1B98E0","black"),
  color=~Color,
  marker=list(
    showscale=F,
    size=~Size,
    reversescale=F
  ) 
)
fig

colors="YlOrRd"

colors = c("#1B98E0","black")

原始答案: 只需在代码末尾添加:%>% hide_colorbar

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()