Geom_map boundaries in ggplot2 - revised

I have a similar problem with @Mike in this question . The question is how to set the colors of the contours of the areas on the chart of the map.

The proposed solution is to add geom_polygonto build over the borders. This works as long as the entire area is built. When you try to limit the subzone, the polygons are weird (apparently because some vertices are removed). Using a standard example geom_map:

# Create example data
ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
values <- data.frame(id = ids, value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5))
positions <- data.frame(
  id = rep(ids, each = 4),
  x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3, 0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
  y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5, 2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)
)

# Plot data
ggplot(values, aes(fill = value)) + 
    geom_map(aes(map_id = id), map = positions) +
    geom_polygon(aes(x,y,group=id), fill = NA, colour = 'red', data = positions) +
    expand_limits(positions) +
    ylim(0, 3)

A possible workaround is to use the color aesthetics in geom_map, and then manually select the outline color using scale_colour_manualas follows:

ggplot(values, aes(fill = value)) + 
    geom_map(aes(map_id = id, colour = 'white'), map = positions) +
    scale_colour_manual(values=c('white')) +
    expand_limits(positions) +
    ylim(0, 3)

I have two questions:

  • geom_polygon , ?
  • , ?

. .

Did not work properly using geom_polygonWorks but is not very elegant

+5
1

, , . x y xlim ylim . , , .

coord_cartesian, x y . "" , , .

, ylim + coord_cartesian(ylim = c(0,3)).

+8

All Articles