hafen / geofacet

R package for geographical faceting with ggplot2
https://hafen.github.io/geofacet/
Other
336 stars 43 forks source link

The values of the specified facet_geo column 'code' do not match any column of the specified grid #182

Open guzmart opened 4 years ago

guzmart commented 4 years ago

I've been using the mx_state_grid2 grid, but it stopped working with a specific tbl_df.

facet_geo(~ code, grid = "mx_state_grid2", label = "code_abbr")

Both codes in each df are the same

`levels(as.factor(encig_electoral$code))

[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" [18] "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31" "32"`

`levels(as.factor(mx_state_grid2$code))

[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" [18] "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31" "32"`

And the error disclaimer is: `

Error: The values of the specified facet_geo column 'code' do not match any column of the > > specified grid.`

There are no NA's in my code column.

Thanks in advance

hafen commented 4 years ago

Interesting. Can you provide a reproducible example?

guzmart commented 4 years ago

data.zip

This is my first reproducible example ever, so sorry in advance if I forget something.

`

Packages

require(geofacet) require(tidyverse)

Does work

dd <- readRDS("does_work.RDS") class(dd$code) levels(as.factor(dd$code)) class(mx_state_grid2$code) levels(as.factor(mx_state_grid2$code))

ggplot(dd %>% filter(Institucion == "presSec", fecha != 2011), aes(x=fecha, y = value, color = reorder(mag, -orden), fill = reorder(mag, -orden), ymin=value-2.58se, ymax=value+2.58se, label = paste0(round(value * 100), "%"))) + geom_line() + geom_ribbon(alpha=.4,linetype=0, size = 1.5) + facet_geo(~ code, grid = "mx_state_grid2", label = "code_abbr")

Does not work

d <- readRDS("does_not_work.RDS") class(d$code) levels(as.factor(d$code)) class(mx_state_grid2$code) levels(as.factor(mx_state_grid2$code))

ggplot() + geom_col(data=d, aes(x=frecuencia_corrupción_presSec, y=frecuencia_corrupción_hospiPu, color= code_abbr), show.legend = F) + facet_geo(~ code, grid = "mx_state_grid2", label = "code_abbr")

Error: The values of the specified facet_geo column 'code' do not match any column of the specified grid.

sessionInfo()

R version 3.5.3 (2019-03-11) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS Mojave 10.14.6

Matrix products: default BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] tidyverse_1.2.1 geofacet_0.1.11 forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3
[6] purrr_0.3.2 readr_1.3.1 tidyr_1.0.0 tibble_2.1.3 ggplot2_3.2.1

loaded via a namespace (and not attached): [1] tidyselect_0.2.5 sf_0.8-0 haven_2.1.1 lattice_0.20-38
[5] rnaturalearth_0.1.0 colorspace_1.4-0 vctrs_0.2.0 rlang_0.4.2
[9] e1071_1.7-3 pillar_1.4.2 withr_2.1.2 glue_1.3.1
[13] DBI_1.0.0 sp_1.3-2 readxl_1.3.1 modelr_0.1.1
[17] jpeg_0.1-8 lifecycle_0.1.0 rgeos_0.4-3 cellranger_1.1.0
[21] munsell_0.5.0 gtable_0.3.0 rvest_0.3.4 labeling_0.3
[25] class_7.3-15 broom_0.5.0 Rcpp_1.0.3 KernSmooth_2.23-15 [29] scales_1.0.0 backports_1.1.4 classInt_0.4-2 jsonlite_1.6
[33] gridExtra_2.3 digest_0.6.20 hms_0.5.1 png_0.1-7
[37] packrat_0.4.9-3 stringi_1.4.3 ggrepel_0.8.1 grid_3.5.3
[41] geogrid_0.1.1 cli_1.1.0 imguR_1.0.3 tools_3.5.3
[45] magrittr_1.5 lazyeval_0.2.2 crayon_1.3.4 pkgconfig_2.0.2
[49] zeallot_0.1.0 ellipsis_0.3.0 xml2_1.2.0 lubridate_1.7.4
[53] assertthat_0.2.1 httr_1.4.0 rstudioapi_0.10 R6_2.4.0
[57] units_0.6-5 nlme_3.1-137 compiler_3.5.3
`

joshua-feldman commented 3 years ago

@guzmart You are receiving this error because you are passing your dataset d as an argument to the geom_col() function, rather than declaring it when you initialise your ggplot object with ggplot(). As a result, the facet_geo() function doesn't know which dataset you are referring to.

Your code should work if you instead run:

ggplot(data=d) +
geom_col(aes(x=frecuencia_corrupción_presSec,
y=frecuencia_corrupción_hospiPu,
color= code_abbr),
show.legend = F) +
facet_geo(~ code, grid = "mx_state_grid2", label = "code_abbr")

Tagging @hafen so he can close this issue.