0

In R, I have hexbins / tilegrams representing jurisdictions of a Canadian province, but the ID labels of hexbins are sitting on the polygons' corners, not inside them.

The closest I got to the desired chart:

enter image description here

I have been messing with geom_polygon, geom_text and geom_label and a few others, but nothing will do. I have seen people use rgeos for doing this with US statebins but I am guessing this is not necessary in my case, as R does recognise that my hexbins have some ID attached to them, it's just placing them not where I want them. I also tried changing the position argument within geoms.

tileGram_f <- ggplot2::fortify(tileGram2) 
tileGram_f <- plyr::join(tileGram_f, tileGram2@data[,c("id", "tile_region")],by="id")

names(tileGram_f)[names(tileGram_f)=="tile_region"] <- "Country"

ggplot(tileGram_f) +
  geom_polygon(aes(long, lat, group = group), 
               fill = "yellow", color = "blue", show.legend = FALSE, size = 1) + 
  coord_equal() + 
  geom_text(data = tileGram_f, 
            aes(x = long, y = lat, label = Country),
            size = 8, color = "red", check_overlap = TRUE) +
  theme(panel.background = element_blank(), 
        axis.title = element_blank(), 
        axis.text = element_blank(), 
        axis.ticks = element_blank())

The hexbins' IDs are made visible by geom_text() or geom_label() successfully (ID numbers, 0-21), but they are placed on each of a polygon / hexbin corner. I want them only ONCE per hexbin and inside it, not around it.

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • 1
    It seems that you might need to finding the coordinates of each center of the poligons and use those for your geom_text – c1au61o_HH Apr 25 '19 at 05:43
  • If `tileGram2` is a spatial object, finding a centroid may not be that difficult. – Roman Luštrik Apr 25 '19 at 06:56
  • Been able to find the centroids / coordinates, both ways below worked, but I have yet to combine those centroids with ggplot2 as it keeps telling me that it must be of length 1 or 21. `# from rgeos - this below trueCentroids = gCentroid(tileGram2,byid=TRUE) plot(tileGram2) points(coordinates(tileGram2),pch=1) points(trueCentroids,pch=2) trueCentroids trueCentroidsDF <- as.data.frame(trueCentroids) # or maybe with coordinates() ? trueCoordinates <- coordinates(tileGram2) colnames(trueCoordinates) <- c('x', 'y') trueCoordinates <- as.data.frame(trueCoordinates)` – EvetsLefurac Apr 25 '19 at 16:09
  • Ok I've got help from the most popular answer in this question [here](https://stackoverflow.com/questions/22038640/labeling-center-of-map-polygons-in-r-ggplot). In `geom_text`, my `label` argument was `names(trueCoordinates)`, but it was giving me this length error above. My error was to use `names()` instead or `rownames()` ... oops! I have my desired chart now, thanks for the leads guys ! – EvetsLefurac Apr 25 '19 at 16:31
  • For reference, code amendment, finding coordinates : `trueCoordinates <- coordinates(tileGram2) colnames(trueCoordinates) <- c('x', 'y') trueCoordinates <- as.data.frame(trueCoordinates) trueCoordinates$id = rownames(trueCoordinates)` geom_text resolution : `geom_text(data = trueCoordinates, aes(x = x, y = y, label = rownames(trueCoordinates)))` – EvetsLefurac Apr 25 '19 at 16:35
  • @EvetsLefurac If you have additional information you'd like to clarify, please edit that into your question. If you have solved your problem, you can pose that as an answer and accept it (after a number of hours, I think). Long code chunks in comments are hard to read, at the best of times. – Z.Lin Apr 26 '19 at 09:46

0 Answers0