Page 1 of 1
Choropleth map
Posted: Sun May 01, 2022 5:03 pm
by rovargasc
Hello. I'm trying to do a choropleth map but I have the following issue:
- error.png (15.1 KiB) Viewed 3650 times
I woud like to know what am I doing wrong.
Here is my code:
- code1.png (51.72 KiB) Viewed 3650 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 (22.92 KiB) Viewed 3650 times
I would like to know how to fix that issue. Thanks in advance!
Re: Choropleth map
Posted: Wed May 04, 2022 1:34 am
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
Re: Choropleth map
Posted: Wed May 04, 2022 1:04 pm
by rovargasc
Ohh got it. Thanks! I fixed that, but now I have other problem:
- error2.png (50.79 KiB) Viewed 3605 times
I tried to fix it with this code:
- code3.png (29.37 KiB) Viewed 3605 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!
Re: Choropleth map
Posted: Wed May 04, 2022 10:50 pm
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 (84.4 KiB) Viewed 3589 times
Cheers,
Maurizio
Re: Choropleth map
Posted: Fri May 06, 2022 12:48 pm
by rovargasc
Thanks MAgojam! I tried your code, but I'm still having some problems, check it out:
- error3.png (38.85 KiB) Viewed 3554 times
This is how my data looks like:
- datamap.png (54.32 KiB) Viewed 3554 times
Re: Choropleth map
Posted: Fri May 06, 2022 8:12 pm
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
Re: Choropleth map
Posted: Fri May 06, 2022 11:48 pm
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!
Re: Choropleth map
Posted: Sat May 07, 2022 5:38 pm
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 (91.34 KiB) Viewed 3504 times
I think you can go on to complete it (you have my link for that).
Cheers,
Maurizio
Re: Choropleth map
Posted: Mon May 09, 2022 7:41 pm
by rovargasc
Thanks Maurizio!