'Cant change scale using TMAP and R shiny
I have a database that looks like this: https://i.stack.imgur.com/SUQdw.png
and here are the files needed to execute this program: https://drive.google.com/file/d/1Vr5ae5At-OfwVp9LckfCyRPJAVp9IMoE/view
here is the map generated: https://vincentchangeon.shinyapps.io/carte/
here is the code
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(dplyr)
library(sf)
library(ggplot2)
library(tmap)
library(leaflet)
library(stringi)
library(tmaptools)
library(mapview)
library(shiny)
library(rsconnect)
base= read.csv("bddlongsubstance.csv", stringsAsFactors = FALSE, encoding = "UTF-8", sep= ",", dec= ".",colClasses=c("code_postal_acheteur"="character"))
base= data.frame(base)
base$code_postal_acheteur= as.factor(base$code_postal_acheteur)
names(base)[1]= "ID"
codes_postaux= st_read(dsn = "codes_postaux_V5/codes_postaux_region.shp",
layer = "codes_postaux_region",
quiet = TRUE) %>%
select(ID, LIB, DEP)
codes_postaux$LIB= stri_encode(codes_postaux$LIB, from= "ISO-8859-1", to= "utf8")
fusion= codes_postaux %>%
left_join(base[1:19], by= "ID") %>%
st_transform(2154)
fusion_vars <- setdiff(names(fusion), c("ID", "LIB", "DEP"))
ui <- fluidPage(
tmapOutput("map"),
selectInput("var", "Variable", fusion_vars)
)
server <- function(input, output, session) {
output$map <- renderTmap({
tm_basemap("CartoDB.Voyager")+ tm_shape(shp= fusion)+
tm_borders("black", lwd= 0.3, alpha= 0.6)+
tm_layout(title = "Quantité de substances phytopharmaceutiques achetées par année et par catégorie (en kilogrammes)", title.position = c("center", "bottom"), legend.bg.color = "white", legend.bg.alpha = 0.4)+
tm_scale_bar(position = c("left", "bottom"))+
tm_view(view.legend.position = c("right", "bottom"))
})
observe({
var <- input$var
tmapProxy("map", session, {
tm_remove_layer(401) +
tm_shape(fusion) +
tm_polygons(var, zindex = 401)
})
})
}
app <- shinyApp(ui, server)
the problem i face is that the scale in the map is meaningless, instead of 0 to 50 000, 50 000 to 100 000 ... I would like 0 to 100, 100 to 200, 200 to 400, 400 to 2000, 2000 to 4000, 4000 to 8000, 8000 to 6000.
I tried many times using tm_fill() but it never works, it doesnt generate an error but it just doesnt do anything.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
