rstudio / leaflet

R Interface to Leaflet Maps
http://rstudio.github.io/leaflet/
Other
805 stars 509 forks source link

Feature Request: Better Default Handling of Color #80

Closed zachcp closed 9 years ago

zachcp commented 9 years ago

Hi Rstudio Team,

Thanks for your great work on this package.

I'd like to request a feature to allow default color handling for any column name that is passed via the scripting interface. Right now the color argument works well when there is a string, a vector of colors, or a dataframe column of colors. However if you simply pass the name of any old column leaflet cannot handle the data. To me, a best-guess representation of color in the spirit of ggplot is desirable where leaflet can detect if the column is a string, a categorical variable, or a continuous variable.

library(leaflet)

#dataframe with lat/lon and a categorical and continuous variable
df = data.frame(Lat = 1:10, 
                Long = rnorm(10),
                CatVar = sample(c("A","B","C"), 10, replace = T),
                ConVar = rnorm(10))

#basemap
m = leaflet(df) %>% addTiles()

# these work
m %>% addCircles(color="red")
m %>% addCircles(color = topo.colors(10,alpha = NULL ))

#it would be great for these to have defaults as well
m %>% addCircles(color=~CatVar)
m %>% addCircles(color=~ConVar)

thanks, zach cp

jcheng5 commented 9 years ago

So, we made a conscious choice not to do this--@hadley can correct me if I'm mistaken, but I believe a great source of ggplot2's complexity both in terms of user comprehension and implementation, is due to needing to distinguish between scaled and unscaled values.

However, to make it easier to work with colors, we've included a handful of color functions to concisely express your intent--our hope was that this is easy enough that you won't miss ggplot-style automatic color scaling too much. If you're aware of these and it still isn't convenient enough for your tastes, please feel free to reopen this issue.

I'm still working on the docs for the color functions, but the function reference already exists and has examples: ?colorNumeric

Here are a couple more examples that build off yours:

m %>% addCircles(color=~colorFactor("RdYlBu", NULL)(CatVar))
m %>% addCircles(color=~colorNumeric("Greens", NULL)(ConVar))

# Share a color scale between two continuous variables
pal <- colorQuantile("Blues", domain = c(df$ConVar1, df$ConVar2))
m %>%
  addCircles(color = ~pal(ConVar1)) %>%
  addCircles(color = ~pal(ConVar2))
zachcp commented 9 years ago

Thanks for your quick reply @jcheng5 . Seems like fine logic to me and for my usecase I can write a little helper function that will distinguish between my common columns and plot accordingly.

As a small counterargument to your choice, I would suggest that the first thing people will want to do when looking at a new dataset is to color by some variable and having to choose a color scheme is another barrier. However, I was unaware of your helper functions which look to ease the problem considerably.

jcheng5 commented 9 years ago

Perhaps we could make an exception for color. It does seem like numeric() and factor() are quite obviously not colors, so there's no danger of mistaking scaled for unscaled data.