Choropleth map

Everything related to the development of modules in jamovi
Post Reply
rovargasc
Posts: 10
Joined: Tue Oct 26, 2021 9:22 pm

Choropleth map

Post by rovargasc »

Hello. I'm trying to do a choropleth map but I have the following issue:

error.png
error.png (15.1 KiB) Viewed 3446 times
I woud like to know what am I doing wrong.

Here is my code:
code1.png
code1.png (51.72 KiB) Viewed 3446 times

I commented those lines because I tried in the 2 ways but neither works

This is the way how I tried to do the plot:
code2.png
code2.png (22.92 KiB) Viewed 3446 times
I would like to know how to fix that issue. Thanks in advance!
User avatar
jonathon
Posts: 2613
Joined: Fri Jan 27, 2017 10:04 am

Re: Choropleth map

Post by jonathon »

hi,

there's an R concept you're missing. you've defined 'long' inside of .run() ... but you need to use a mechanism to get access to 'long' from inside of .plot() ... the way we normally pass these things around, is using image$setState(...) and then we can access it inside .plot() with image$state

cheers

jonathon
rovargasc
Posts: 10
Joined: Tue Oct 26, 2021 9:22 pm

Re: Choropleth map

Post by rovargasc »

Ohh got it. Thanks! I fixed that, but now I have other problem:
error2.png
error2.png (50.79 KiB) Viewed 3401 times
I tried to fix it with this code:
code3.png
code3.png (29.37 KiB) Viewed 3401 times
I don't know what am I doing wrong. I'm still new in creating modules.

I really appreciate your help. Thanks in advance!
User avatar
MAgojam
Posts: 421
Joined: Thu Jun 08, 2017 2:33 pm
Location: Parma (Italy)

Re: Choropleth map

Post by MAgojam »

Hey @rovargasc,

maybe my code can help you (similar to what you are doing) that I made on the fly some time ago and left in a drawer, to improve it later.

Code: Select all

## file: mappa.b.r per modulo jamovi coromap
## data: 17/03/2022
mappaClass <- if (requireNamespace('jmvcore', quietly=TRUE)) R6::R6Class(
    "mappaClass",
    inherit = mappaBase,
    private = list(
        .run = function() {
            long <- self$options$long
            lat <- self$options$lat
            group <- self$options$group
            misure <- self$options$misure
            
            varNames <- c(long, lat, group, misure)
            
            if (is.null(long) || is.null(lat) || is.null(group) || is.null(misure))
                return()
            
            Dataset <- jmvcore::select(self$data, varNames)
         
            image <- self$results$plot
            image$setState(Dataset)
            
            },
            .plot=function(image, ...){

                Dataset <- image$state

                PlotData <- Dataset[,1:4]
                colnames(PlotData) <- c('long', 'lat', 'group', 'misure')

                p <- ggplot2::ggplot() +
                     ggplot2::geom_polygon(data=PlotData, 
                                           aes(x=long, 
                                               y=lat, 
                                               group=group, 
                                               fill=misure), 
                                               color='white', 
                                           size = 0.2) +
                     ggplot2::coord_map('polyconic') +
                     ggplot2::scale_fill_gradient2(low="#559999", 
                                                   mid="grey90", 
                                                   high="#BB650B",
                                                   midpoint=median(misure))+
                     ggplot2::scale_fill_viridis_c()
                
                print(p)
                TRUE

        }
    )
)
coromap.PNG
coromap.PNG (84.4 KiB) Viewed 3385 times
Cheers,
Maurizio
rovargasc
Posts: 10
Joined: Tue Oct 26, 2021 9:22 pm

Re: Choropleth map

Post by rovargasc »

Thanks MAgojam! I tried your code, but I'm still having some problems, check it out:
error3.png
error3.png (38.85 KiB) Viewed 3350 times
This is how my data looks like:
datamap.png
datamap.png (54.32 KiB) Viewed 3350 times
User avatar
MAgojam
Posts: 421
Joined: Thu Jun 08, 2017 2:33 pm
Location: Parma (Italy)

Re: Choropleth map

Post by MAgojam »

Hey @rovargasc,
if you give me a link (GitHub) to your code, I can take a look and maybe help you.

Saudações,
Maurizio
rovargasc
Posts: 10
Joined: Tue Oct 26, 2021 9:22 pm

Re: Choropleth map

Post by rovargasc »

Okay. This is the link: https://github.com/rovargasc/coroplethmap
These are the files, I tried to do what you have done, but it doesn't work for me...

I really appreciate your help. Thanks!
User avatar
MAgojam
Posts: 421
Joined: Thu Jun 08, 2017 2:33 pm
Location: Parma (Italy)

Re: Choropleth map

Post by MAgojam »

Hey @rovargasc,
I took a look at your module on GitHub and made a few changes.
Now your form does this:
coromap.PNG
coromap.PNG (91.34 KiB) Viewed 3300 times
I think you can go on to complete it (you have my link for that).

Cheers,
Maurizio
rovargasc
Posts: 10
Joined: Tue Oct 26, 2021 9:22 pm

Re: Choropleth map

Post by rovargasc »

Thanks Maurizio!
Post Reply