Hi
I need my own colors in the colorbar of a plot_geo
(not the ones that result from the default interpolation). See this reproducible example:
library(plotly)
df2 <- data.frame(COUNTRY=c("Algeria", "Argentina", "Australia", "Austria", "Bangladesh"),
CODE=c("DZA", "ARG", "AUS", "AUT", "BGD"),
pcnt=c(1.29, 2.15, 5.17, 1.29, 0.43))
breaks <- pretty(df2$pcnt)
## [1] 0 1 2 3 4 5 6
colors <- colorRampPalette(RColorBrewer::brewer.pal(9, "RdBu"))(length(breaks))
## [1] "#B2182B" "#E0775E" "#FAC9B0" "#F7F7F7" "#BCDAEA" "#5DA3CC" "#2166AC"
p <- plot_geo(df2) %>%
add_trace(z=~pcnt, color=~pcnt,
colors=colors, locations = ~CODE,
autocolorscale=F) %>%
colorbar(title='Contribution [%]') %>%
layout(geo=list(showframe=T, showcoastlines=T, projection=list(type='Mercator')))
htmlwidgets::saveWidget(p, file="test.html") # open test.html in browser
The colors of the countries are perfect but I dont want the interpolation in the colorbar.
Is there a way to disable the default interpolation of the colors for the colorbar and provide some specific breaks
and col
instead? Something that looks like
library(fields)
image.plot(diag(df2$pcnt), breaks=breaks,
col=colorRampPalette(RColorBrewer::brewer.pal(9, "RdBu"))(length(breaks) - 1))
I played around with the colorscale
argument as mentioned here, but it did not work.
Thanks a lot for answer!
Chris
version R version 3.5.2 (2018-12-20)
os Arch Linux
plotly * 4.8.0.9000 2019-03-04 [1] Github (ropensci/plotly@6aa3717)
ggplot2 * 3.1.0 2018-10-25 [1] CRAN (R 3.5.2)