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

Format the 'to'-break values differently #258

Closed
mcSamuelDataSci opened this issue Nov 13, 2018 · 5 comments
Closed

Format the 'to'-break values differently #258

mcSamuelDataSci opened this issue Nov 13, 2018 · 5 comments

Comments

@mcSamuelDataSci
Copy link

This issue may be related to the issue you already solved that I submitted #249. And, I may be doing some weird stuff, and am admittedly mapping a very awkward and skewed "distribution", BUT, the plot mode map is more-or-less clear and certainly interpretable.

ex_plot

The same map in view mode has some important problem with the legend, such that a color in the map does not appear in the legend, so it become uninterpretable.

ex_view

Here is some code that reproduces the exact problem, and has some comments to show what I am doing.

# load needed packages for the current R session
library(dplyr)
library(tigris)   
library(tmap)
library(classInt)  
library(RColorBrewer)

# Create a California census tract object
options(tigris_class = "sf")  
caTract  <- tracts(state = "CA", cb = TRUE)  

# Read .csv file from GitHub that links tracts to "communities" (MSSAs)
link_tract_community  <- read.csv("https://raw.githubusercontent.com/mcSamuelDataSci/CACommunityBurden/master/myCBD/myInfo/cbdLinkCA.csv",colClasses = "character")

# Merge/join census tract shape file with tract-community link file
caTract <- caTract %>% 
  left_join(link_tract_community, by="GEOID")

# Create a new "shape file" by grouping the tracts by communities. 
shape_Comm  <- caTract %>% 
  group_by(county,comID) %>% 
  summarize()  %>% ungroup()

# Read ("fake") bit of California death data
githubURL <- "https://raw.githubusercontent.com/mcSamuelDataSci/CACommunityBurden/master/myCBD/myData/fake/conditionX.RDS"
download.file(githubURL,"temp.rds", method="curl")
community_data <- readRDS("temp.rds")

# Merge shape file object to the community death data object.
map_work <-  left_join(shape_Comm,community_data, by=c("county","comID"))

map_work$Ndeaths[is.na(map_work$Ndeaths)] <- 0

# Out of context, this line below might seem bad/weird (i.e. why not put in the equivalent in the tm_polygons below),
#    but it is actually important for my project
myBreaks  <- classIntervals(map_work$Ndeaths,style="quantile",breaks=NULL,n=5)$brks
myPal     <- rev(brewer.pal(5,"RdYlBu"))

xmap <- tm_shape(map_work) + tm_polygons(col="Ndeaths",palette = myPal, style="fixed",breaks=myBreaks, 
                              interval.closure="right",
                              legend.reverse=T
                              )

# this looks "fine".  Yes, it is "bad" that there are three colors for "0", but at least it is
#   interpretable
tmap_mode("plot")
xmap

# this is where something is wrong:
#  "legend.reverse=T" is ignored
#  there is orange in the map, but no orange in the legend
#  There are 3 levels in the legend, not 5
tmap_mode("view")
xmap

# yes, the distribution of this variable being mapped is very skewed (sort of by definition, since
# all values less that 11 are supressed and shown as 0)
table(map_work$Ndeaths)

I sure appreciate any help you can provide!

@mcSamuelDataSci
Copy link
Author

Hi Martijn-
I know this is a odd little issue that might only arise with a small, highly skewed, distribution being mapped. Nevertheless, if there is any "proper" fix that could implement for this, that would be most appreciated. If not, I can do some "work arounds" on my end.
Thanks! Michael

@mtennekes
Copy link
Member

Hi Michael,

This was a common problem: leaflet::addLegend has its own interpretation of which items are included.
I did solve the problem by calling addLegend manually. So it should work now!

Could you do me a favor? Could you test the legend behavior in interactive mode, also for other data/map types?

Thx!
Martijn

@mcSamuelDataSci
Copy link
Author

Martijn-
Works great based on a bit of testing! Thank you so much. I will add an acknowledgement of your assistant on our project site, unless you would prefer I don't for any reason?

Also, I will dig into it more myself, but can you direct me to anything I might look at such that a legend might show something along the lines of 0-4.9, 5.0-9.9 etc. (rather than 0-5, 5-10, etc) even if the data is actually continuous? Perhaps no simple way.

Thanks again, Michael

@mtennekes
Copy link
Member

You're welcome, Michael!

There is no easy way to format the numbers like that. There is an argument legend.format, which can be used to format the legend numbers, also with a custom function. However, this function does not distinguish 'from' and 'to' numbers.

I can image applications for which this could also be handy, say for an integer variable from 1 to 100, which is considered as continuous variable, and where the preferred breaks are 1-9, 10-19, 20-29, etc., rather than 1-10, 10-20, 20-30, etc.

@mtennekes mtennekes changed the title problem with view mode map legend in "unusual" situation Format the 'to'-break values differently Nov 29, 2018
@Nowosad
Copy link
Member

Nowosad commented Apr 26, 2020

See new improvements in this area at https://github.com/mtennekes/tmap/issues/406#issuecomment-610559092.

@Nowosad Nowosad closed this as completed Apr 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants