Closed bfree-ngs closed 3 years ago
Thanks for opening a new issue and providing your code, a figure, and the session info.
I can see that you are using ggrepel 0.8.2 from CRAN.
Please consider trying these suggestions:
Try ggplot2::geom_text()
instead of ggrepel::geom_text_repel()
. Does that work as expected?
Try installing the GitHub version of ggrepel with devtools::install_github("slowkow/ggrepel")
. Does that version have the same problem?
Share a complete reproducible example that includes a small dataset, so others can run exactly the same code that you have run. This will help developers to reproduce your issue and to find a fix for it.
Thanks, Kamil, for responding.
I had tried ggplot2::geom_text()
instead of repel. And while the sites are labeled, it was not to the desired effect. Ran devtools::install_github("slowkow/ggrepel")
yesterday and nothing appeared to be alleviated and today am getting the following error:
Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]])
: namespace ‘processx’ 3.3.1 is being loaded, but >= 3.4.0 is required
I have version 3.6 installed, so not exactly sure what is going on, sorry.
Can find data (without the coral data, as they are too large) and the overall code. Hopefully that helps.
Much appreciated for your guidance, Brian
Hi Brian, we don't need the coral data. Any data will do, as long as we can reproduce the issue with ggrepel. I don't ever use geom_sf()
, so I'm not familiar with the type of data you need for that function. If we have a small reproducible code example that comes with a small dataset, then I can run it on my computer to find a fix.
Regarding the new error, please try to close R and open a new R session. Then run the package installation again (for ggrepel or processx, or both).
Let me know when you've tested that the latest version of ggrepel from GitHub has a problem. Please do share the code, output, errors, sessionInfo, etc.
Hi Kamil. This is the sample code, and the sample data (province and survey site) are likewise here.
Attached are the code (the long code, not the sample code) and the output (with the x-axis still being where the text goes). The second image is if the geom_text function is used instead (the site names appear with some nudging).
Session information from running the code and loading ggrepel from the GitHub repository:
> sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] pacman_0.5.1 tidyr_0.8.3 sf_0.9-3 scales_1.0.0 rgeos_0.5-3 rgdal_1.4-4
[7] RColorBrewer_1.1-2 raster_2.9-23 sp_1.3-1 metR_0.7.0 ggspatial_1.1.3 ggsn_0.5.0
[13] fasterize_1.0.2 dplyr_0.8.1 ggrepel_0.8.2 ggplot2_3.1.1
loaded via a namespace (and not attached):
Error in x[["Version"]] : subscript out of bounds
In addition: Warning message:
In FUN(X[[i]], ...) :
DESCRIPTION file of package 'fansi' is missing or broken
Any advice you have is greatly appreciated. Thank you.
I can see that you are using ggrepel version 0.8.2, which is the current CRAN version. The GitHub version has a lot of additional features.
After installing the new version, please see if you can run the code examples from these issues:
Hi Kamil,
Sorry, but I have followed the instructions to install the GitHub version for ggrepel, but when I load the package, it still says that the version is 0.8.2.
devtools::install_github("slowkow/ggrepel") Downloading GitHub repo slowkow/ggrepel@master These packages have more recent versions available. Which would you like to update?
1: All 2: CRAN packages only 3: None 4: ggplot2 (3.1.1 -> 7f88a569e...) [GitHub] 5: backports (1.1.4 -> 1.1.8 ) [CRAN] 6: pillar (1.4.1 -> 1.4.4 ) [CRAN] 7: pkgbuild (1.0.3 -> 1.0.8 ) [CRAN] 8: pkgconfig (2.0.2 -> 2.0.3 ) [CRAN] 9: pkgload (1.0.2 -> 1.1.0 ) [CRAN] 10: prettyunits (1.0.2 -> 1.1.1 ) [CRAN] 11: ps (1.3.0 -> 1.3.3 ) [CRAN] 12: R6 (2.4.0 -> 2.4.1 ) [CRAN] 13: Rcpp (1.0.1 -> 1.0.4.6 ) [CRAN] 14: rlang (0.4.4 -> 0.4.6 ) [CRAN] 15: rstudioapi (0.10 -> 0.11 ) [CRAN] 16: scales (1.0.0 -> 1.1.1 ) [CRAN] 17: testthat (2.3.1 -> 2.3.2 ) [CRAN] 18: tibble (2.1.2 -> 3.0.1 ) [CRAN] 19: vctrs (0.1.0 -> 0.3.1 ) [CRAN] 20: withr (2.1.2 -> 2.2.0 ) [CRAN]
Enter one or more numbers separated by spaces, or an empty line to cancel 1: 1
library(ggrepel) sessionInfo() R version 3.6.0 (2019-04-26) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] ggrepel_0.8.2 ggplot2_3.1.1
Brian Free Marine Spatial Scientist World Wildlife Fund / National Geographic Society WWF: 1250 24th Street | Washington, D.C. 20037 NGS: 1145 17th Street | Washington, D.C. 20036
WWF: +1 202.495.4163 Mobile: +1 202.480.0573 E-mail (WWF): brian.free@wwfus.org E-mail (NGS): bfree_c@ngs.org
On Wed, Jun 24, 2020 at 9:16 AM Kamil Slowikowski notifications@github.com wrote:
I can see that you are using ggrepel version 0.8.2, which is the current CRAN version. The GitHub version has a lot of additional features.
After installing the new version, please see if you can run the code examples from these issues:
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/slowkow/ggrepel/issues/165#issuecomment-648813444, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM6H3XN6SYJJL4UBWS5F3CLRYH4ERANCNFSM4OFUIVTA .
Kamil,
For issue 89, I get the same map that your code produces. So not exactly sure what is occurring. I had previously tried the geom_sf_text.
Brian Free Marine Spatial Scientist World Wildlife Fund / National Geographic Society WWF: 1250 24th Street | Washington, D.C. 20037 NGS: 1145 17th Street | Washington, D.C. 20036
WWF: +1 202.495.4163 Mobile: +1 202.480.0573 E-mail (WWF): brian.free@wwfus.org E-mail (NGS): bfree_c@ngs.org
On Wed, Jun 24, 2020 at 9:29 AM Free, Brian bfree_c@ngs.org wrote:
Hi Kamil,
Sorry, but I have followed the instructions to install the GitHub version for ggrepel, but when I load the package, it still says that the version is 0.8.2.
devtools::install_github("slowkow/ggrepel") Downloading GitHub repo slowkow/ggrepel@master These packages have more recent versions available. Which would you like to update?
1: All 2: CRAN packages only 3: None 4: ggplot2 (3.1.1 -> 7f88a569e...) [GitHub] 5: backports (1.1.4 -> 1.1.8 ) [CRAN] 6: pillar (1.4.1 -> 1.4.4 ) [CRAN] 7: pkgbuild (1.0.3 -> 1.0.8 ) [CRAN] 8: pkgconfig (2.0.2 -> 2.0.3 ) [CRAN] 9: pkgload (1.0.2 -> 1.1.0 ) [CRAN] 10: prettyunits (1.0.2 -> 1.1.1 ) [CRAN] 11: ps (1.3.0 -> 1.3.3 ) [CRAN] 12: R6 (2.4.0 -> 2.4.1 ) [CRAN] 13: Rcpp (1.0.1 -> 1.0.4.6 ) [CRAN] 14: rlang (0.4.4 -> 0.4.6 ) [CRAN] 15: rstudioapi (0.10 -> 0.11 ) [CRAN] 16: scales (1.0.0 -> 1.1.1 ) [CRAN] 17: testthat (2.3.1 -> 2.3.2 ) [CRAN] 18: tibble (2.1.2 -> 3.0.1 ) [CRAN] 19: vctrs (0.1.0 -> 0.3.1 ) [CRAN] 20: withr (2.1.2 -> 2.2.0 ) [CRAN]
Enter one or more numbers separated by spaces, or an empty line to cancel 1: 1
library(ggrepel) sessionInfo() R version 3.6.0 (2019-04-26) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] ggrepel_0.8.2 ggplot2_3.1.1
loaded via a namespace (and not attached): Error in x[["Version"]] : subscript out of bounds In addition: Warning message: In FUN(X[[i]], ...) : DESCRIPTION file of package 'glue' is missing or broken
Brian Free Marine Spatial Scientist World Wildlife Fund / National Geographic Society WWF: 1250 24th Street | Washington, D.C. 20037 NGS: 1145 17th Street | Washington, D.C. 20036
WWF: +1 202.495.4163 Mobile: +1 202.480.0573 E-mail (WWF): brian.free@wwfus.org E-mail (NGS): bfree_c@ngs.org
On Wed, Jun 24, 2020 at 9:16 AM Kamil Slowikowski < notifications@github.com> wrote:
I can see that you are using ggrepel version 0.8.2, which is the current CRAN version. The GitHub version has a lot of additional features.
After installing the new version, please see if you can run the code examples from these issues:
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/slowkow/ggrepel/issues/165#issuecomment-648813444, or unsubscribe https://github.com/notifications/unsubscribe-auth/AM6H3XN6SYJJL4UBWS5F3CLRYH4ERANCNFSM4OFUIVTA .
OK, I think we have two different issues here:
The GitHub version of ggrepel is not installing correctly on your system, not sure why. Please feel free to create a new issue if you'd like us to troubleshoot. I have a feeling that you are getting some errors after running the installation command. Please include the full command, the full output, etc. in the new issue.
The result you get for one code snippet with geom_sf()
is different than for another one. This might be a ggrepel bug, but I'm not sure about it. Maybe it has something to do with the data formatting? We can continue discussing in this issue, but we should make sure to use the latest version of the code in the discussion.
By the way, I'm not seeing any of the images in the last few posts. Could I please ask you to visit this URL and edit your posts so we can see what you see?
Hi Kamil,
Sorry about the previous errors. This is the image that was included earlier.
Is the latest development version 0.9.0? If so, interestingly, now ggrepel in the code produces nothing on the plot.
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] tidyr_1.1.0 sf_0.9-4 scales_1.1.1 rgeos_0.5-3 rgdal_1.5-10 RColorBrewer_1.1-2
[7] raster_3.1-5 sp_1.4-2 ggspatial_1.1.3 ggsn_0.5.0 ggrepel_0.9.0 ggplot2_3.3.2
[13] fasterize_1.0.2 dplyr_1.0.0 pacman_0.5.1 metR_0.7.0 backports_1.1.8
loaded via a namespace (and not attached):
[1] tidyselect_1.1.0 purrr_0.3.4 lattice_0.20-41 colorspace_1.4-1 vctrs_0.3.1 generics_0.0.2
[7] rlang_0.4.6 e1071_1.7-3 pillar_1.4.4 foreign_0.8-71 glue_1.4.1 withr_2.2.0
[13] DBI_1.1.0 jpeg_0.1-8.1 lifecycle_0.2.0 plyr_1.8.6 stringr_1.4.0 munsell_0.5.0
[19] gtable_0.3.0 RgoogleMaps_1.4.5.3 codetools_0.2-16 maptools_1.0-1 class_7.3-17 Rcpp_1.0.4.6
[25] KernSmooth_2.23-17 classInt_0.4-3 checkmate_2.0.0 farver_2.0.3 digest_0.6.25 rjson_0.2.20
[31] png_0.1-7 stringi_1.4.6 tools_3.6.0 bitops_1.0-6 magrittr_1.5 tibble_3.0.1
[37] crayon_1.3.4 pkgconfig_2.0.3 ellipsis_0.3.1 data.table_1.12.8 lubridate_1.7.9 httr_1.4.1
[43] rstudioapi_0.11 R6_2.4.1 units_0.6-7 ggmap_3.0.0 compiler_3.6.0
Yes, ggrepel 0.9.0 is the version from GitHub.
Here's what I see.
Did you get a chance to try the other examples from the related issues with ggrepel 0.9.0?
Great. Yes, I got a chance to to look at those examples. From issue 111, it would look like that geometry and sf_coordinates are what do the trick for sf features to get text by the the sf point data, in this case.
Any suggestions on getting the size, nudging, or have the lines reach the points would be appreciated.
# Preparing packages
if (!require("pacman")) install.packages("pacman")
# Load packages
pacman::p_load(dplyr,fasterize,ggplot2,ggsn,ggspatial,metR,raster,RColorBrewer,rgdal,rgeos,scales,sf,sp,tidyr)
# get the most updated version of ggrepel
library(ggrepel)
# Setting data directory
gis_dir <- setwd("C:\\Users\\free\\Dropbox (MPAMystery)\\RICOTTA_GIS\\oceans_program\\dom\\fiji_report\\gis_r")
# where to save provinces
province_map_dir <- "C:\\Users\\free\\Dropbox (MPAMystery)\\RICOTTA_GIS\\oceans_program\\dom\\fiji_report\\maps\\province"
# Read data
###########################
# Loading the required data
# Administrative boundary data
fiji <- st_read(dsn = gis_dir, layer = "fiji") # Fiji land administrative boundary
qoliqoli <- st_read(dsn = gis_dir, layer = "qoliqoli") # Qoliqoli (district) boundaries where survey sites occurred
provinces <- st_read(dsn = gis_dir, layer = "province") # Fiji provinces where survey sites occurred
gsr <- st_read(dsn = gis_dir, layer = "gsr") # Great Sea Reef boundary
# Ecological data
fji_coral <- st_read(dsn = gis_dir, layer = "fiji_coral") # Coral data extent in Great Sea Reef
# Rasterize ecological data
# Coral data
coral_temp <- raster(extent(fji_coral),res = 25, crs = fji_coral) # create a template raster with the coral reef extent
coral_rast <- fasterize(fji_coral,coral_temp) # rasterizing the coral data
coral_map <- raster::as.data.frame(coral_rast, xy=T) %>% # Convert to dataframe to have mapped later
dplyr::filter(!is.na(layer)) %>%
setNames(c("longitude", "latitude", "coral")) %>%
mutate(coral = "Coral")
# Historic survey site data
surv_site <- st_read(dsn = gis_dir, layer = "gsr_survey_sites") %>% # all survey sites
# Rearrange columns
dplyr::select(Site,District,Historic_f,Qoliqoli_I,Place,Villages,
Province,Latitude,Longitude,
Province_1,Sub_group,Historic_b,
Past_data_,geometry) %>%
# Remove place, villages, province, sub-group, historic_b, past data
dplyr::select(-Place,
-Villages,
-Qoliqoli_I,
-Province,
-Sub_group,
-Historic_b,
-Past_data_) %>%
# Rename columns
rename(site=Site,
district=District,
surveyor=Historic_f,
latitude=Latitude,
longitude=Longitude,
province=Province_1)
levels(surv_site$surveyor) # get order of the surveyors
levels(surv_site$province) # survey sites were not conducted in Ra Province
# Add Ra province to the provinces field to assist with the map for loops
levels(surv_site$province) <- c(levels(surv_site$province), "Ra")
levels(surv_site$province) # see that Ra now appears for the field
# Quick map of the area
map1 <- ggplot() +
geom_sf(data = fiji, fill = "red") +
geom_sf(data = gsr) +
geom_sf(data = qoliqoli) +
geom_sf(data = provinces, fill = "green") +
geom_sf(data = surv_site) +
scale_x_longitude(breaks = seq(-178,180,2)) +
xlab("Longitude") +
ylab("Latitude") +
ggtitle("Quick map") +
theme_bw()
map1
# Map setup
###########################
## Defining map elements
# Colors
# administrative
land_col <- "#878787"
water_col <- "#a6cee3"
# ecological
coral_col <- "#e31a1c" # red
# unique site colors
eia_col <- "#ffffb3" # light yellow
new_col <- "#fb8072" # light pink
rfc_col <- "#bebada" # light purple
wwf_col <- "#8dd3c7" # light green
# survey site shapes
eia_shape <- 21 # circle
new_shape <- 22 # square
rfc_shape <- 23 # diamond
wwf_shape <- 24 # triangle up
# scale bar
scalebar_ba <- annotation_scale(width_hint = 0.2, # percent of the plot occupied (20%)
pad_x = unit(1.55, "in"), # how much padded from the x=0 position
pad_y = unit(0.05, "in")) # how much padded from the y=0 position
# north arrow
narrow_ba <- annotation_north_arrow(height = unit(0.25, "in"),
width = unit(0.20, "in"),
pad_x = unit(2.4, "in"),
pad_y = unit(0.05, "in"),
style = north_arrow_orienteering(
line_width = 1,
line_col = "black",
fill = c("white", "black"),
text_col = "black",
text_family = "",
text_face = NULL,
text_size = 5,
text_angle = 0))
# map themes
map_theme_ba <- theme(axis.text=element_text(size=8),
axis.title=element_text(size=10),
plot.title=element_text(size=12),
panel.grid.major = element_line(color = "transparent"),
panel.grid.minor = element_line(color = "transparent"),
panel.background = element_rect(fill = water_col),
axis.text.y = element_text(angle = 90, hjust = 0.5),
legend.position = c(0.15,0.9), # alternative = bottom
legend.title = element_blank(), # remove the legend title
legend.text = element_text(size=6), # text size of the descriptor
legend.background = element_rect(fill = "transparent"), # make the box transparent --> "transparent"
legend.box.background = element_rect(fill = "white", color = "#4C84A2"), # white background with blue border
legend.box.margin = margin(1,1,1,1), # add some space between the box and the text
legend.spacing.y = unit(0.025, "in"),
legend.key.size = unit(0.1, "in"), # size of the color box
legend.key = element_rect(fill = "transparent"), # make the background of the key clear
legend.margin = margin(0, 0.0, 0, 0, "in"), # reduce spacing between legend elements
axis.line = element_line(colour = "black"))
# Figure generation
###########################
# get the Ba province
i <- 1
for (i in 1){
# get province
province_do <- provinces[i,]
# get the limits
xlim_prov <- c(xmin = st_bbox(province_do)$xmin-1000, xmax = st_bbox(province_do)$xmax+1000)
ylim_prov <- c(xmin = st_bbox(province_do)$ymin, xmax = st_bbox(province_do)$ymax)
# extract province name
prov_name <- province_do$Name
# x-axis limits
if(i==1){xbreaks <- seq(177,178,0.25)}
# Create the loop for the provincial survery site maps
coral_map_sample <- sample_frac(coral_map,0.01) # display only 1% of coral data to speed up map process
province_survey <- ggplot() +
# load Fiji land
geom_sf(data = fiji, fill = land_col, color = NA) +
# load Great Sea Reef
geom_sf(data = gsr, fill = NA, aes(linetype = "Great Sea Reef"), size = 0.5) +
# load coral data
geom_tile(data = coral_map_sample, aes(x=longitude,y=latitude, color="Coral")) +
# load province
geom_sf(data = province_do, fill = NA, aes(linetype = Name), size = 0.5) +
# load suvery site data
geom_sf(data = surv_site, aes(fill=surveyor, shape=surveyor),show.legend = "point") +
# focus on the area of interest
coord_sf(xlim = xlim_prov,
ylim = ylim_prov) +
# x-axis breaks
#scale_x_longitude(breaks = xbreaks) +
# survey shape
scale_shape_manual(name = "Survey Site",
labels = c("Ba EIA",
"New Site",
"Reef Check",
"WWF"),
values = c(eia_shape,
new_shape,
rfc_shape,
wwf_shape),
guide = guide_legend(override.aes = list(fill = c(eia_col,
new_col,
rfc_col,
wwf_col)))) +
# surveyor fill
scale_fill_manual(labels = c("Ba EIA",
"New Site",
"Reef Check",
"WWF"),
values = c(eia_col,
new_col,
rfc_col,
wwf_col)) +
# Great Sea Reef legend
scale_linetype_manual(name = "Borders",
values = c("solid", "3313"),
guide = guide_legend(override.aes = list(color = c("grey30","grey50"),
shape = c(NA,NA)))) +
# coral legend
scale_color_manual(name = "Benthic habitat",
values = coral_col,
label = "Coral reefs",
guide = guide_legend(override.aes = list(fill = coral_col,
shape = NA))) +
# remove fill symbology
guides(fill = FALSE) +
# repel text of sites in area of interest
ggrepel::geom_text_repel(data = filter(surv_site, province == prov_name),
mapping = aes(x = longitude,
y = latitude,
label = site,
geometry = geometry),
stat = "sf_coordinates",
size = 1,
fontface = "bold",
nudge_x = 20,
nudge_y = 30,
max.iter = 1500) +
# labels + title
labs(x="",y="", title="") +
# theme
theme_bw() +
map_theme_ba
# Export plots
out_survey <- paste0(prov_name,"_survey2.tiff")
ggsave(province_survey, filename=file.path(province_map_dir, out_survey), width=6.5,
height=4.5, units="in", dpi=600, compression = "lzw")
}
Glad you got it working!
To increase font size, try size = 5
instead of size = 1
.
For line segments, try min.segment.length
.
You might also consider copying code from the examples: https://ggrepel.slowkow.com/articles/examples.html
This tweet by @clauswilke may be of interest. He mentions some recent changes to ggplot2 and utility functions that you might find useful.
https://twitter.com/ClausWilke/status/1275943244182978562?s=20
In the currently released ggplot2 version, geom_text_repel()
should work with geom_sf()
as long as you provide geom_text_repel()
with projected coordinates. In the development version, projected coordinates are not needed, you can just use lon for x and lat for y.
If we can provide more help here, please re-open the issue or create a new one with a code example.
The issue that is arising is when trying to use geom_text_repel on a geom_sf. The desired result would be for the site id numbers to appear near the site locations. Currently, however, the text are marking along the x-axis. Thought that the aes for xy might be the issue, but when examining the values for the latitude and longitude, it would not appear that the values are off the plot:
Currently, this is producing the following image:
Version information
R
sessionInfo()