Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor order for colours #138

Closed
SymbolixAU opened this issue Mar 18, 2019 · 4 comments
Closed

Factor order for colours #138

SymbolixAU opened this issue Mar 18, 2019 · 4 comments
Assignees
Milestone

Comments

@SymbolixAU
Copy link
Owner

@SymbolixAU SymbolixAU commented Mar 18, 2019

Keep the order of a factor variable when colouring

@SymbolixAU SymbolixAU self-assigned this Mar 18, 2019
@SymbolixAU SymbolixAU added this to the v0.3.0 milestone Apr 1, 2019
@mountainMath
Copy link

@mountainMath mountainMath commented Jun 11, 2019

Example:

data <- tibble::tibble(lat=c(49.25,49.2),lng=c(-123.1,-123),
                       value=factor(c("A","B"),levels=c("B","A"))) %>%
  sf::st_as_sf(coords = c("lng", "lat"), crs = 4326, agr = "constant")

mapdeck(token = getOption("mapbox_token"), style = mapdeck_style('dark')) %>%
  mapdeck(token = getOption("mapbox_token"), style = mapdeck_style('dark')) %>%
  add_sf(data=data,
         fill_colour="value",
         legend = TRUE,
         radius = 500)

Result:

image

Expected behaviour for factors is a discrete colour scale with level values as labels, instead of continuous colour scale with level order as values.

When cast to character we get character labels, but the order is lost. The levels are ordered as "B" first and the "A", but this is lost when converting to character.

mapdeck(token = getOption("mapbox_token"), style = mapdeck_style('dark')) %>%
  add_sf(data=data %>% dplyr::mutate(value=as.character(value)),
         fill_colour="value",
         legend = TRUE,
         radius = 500)

image

@dcooley dcooley mentioned this issue Jun 12, 2019
1 of 1 task complete
@dcooley
Copy link
Collaborator

@dcooley dcooley commented Jun 12, 2019

If you install the latest version, plus the dependencies, you should now see this working

devtools::install_github("SymbolixAU/colourvalues", force = TRUE )
devtools::install_github("SymbolixAU/spatialwidget", force = TRUE )
devtools::install_github("SymbolixAU/mapdeck", force = TRUE )


library(mapdeck)
library(tibble)
library(sf)

data <- tibble::tibble(
	lat = c(49.25,49.2)
	, lng = c(-123.1,-123)
	, value = factor( c("A","B"), levels = c("B","A"))) %>%
	sf::st_as_sf(coords = c("lng", "lat"), crs = 4326, agr = "constant")

str( data )

set_token( read.dcf("~/.mapbox", fields = "MAPBOX") )

data$value2 <- as.character( data$value )

mapdeck(style = mapdeck_style('dark')) %>%
	add_sf(
		data = data
		, fill_colour = "value"
		, stroke_colour = "value2"
		, tooltip = "value"
		, legend = TRUE
		, radius = 500
	)

Screenshot from 2019-06-12 18-20-10

@mountainMath
Copy link

@mountainMath mountainMath commented Jun 13, 2019

Thanks, that works for me.

One change I noticed is that add_sf does not seem to be as tolerant any more. It used to work with geometry type GEOMETRY, now I need to st_collection_extract("POLYGON") to get it to show up.

@dcooley
Copy link
Collaborator

@dcooley dcooley commented Jun 13, 2019

add_sf() hasn't changed (or I haven't intentionally changed it), GEOMETRY should go through to add_geojson()

Could you open a new issue with an example?

@dcooley dcooley closed this Jun 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.