vignettes/troubleshooting-lines-between-polygons.Rmd
troubleshooting-lines-between-polygons.Rmd
When using geom_polygon
, you might get lines between the polygons. These artifacts don’t happen all the time and seem to occur more often between shapes of the same color and on macOS. This vignette walks through different attempts to resolve them.
This section sets up the data set and output image.
tiles <- return_rhombus(0, 0, -90 + 36, 100, 'T') %>%
substitution() %>%
substitution() %>%
substitution() %>%
substitution() %>%
substitution() %>%
substitution() %>%
substitution() %>%
pivot_down()
ggplot(tiles) +
geom_polygon(aes(x = x,
y = y,
fill = shape,
group = triangle)) +
coord_equal()
Saving this image as a png file and then zooming in shows the following image. We can see the lines between polygons.
The following code snippets attempt to solve this issue. They only show the zoomed-in output. So, the code won’t match the image but does start the process.
color
to aesthetic mapping.
ggplot(tiles) +
geom_polygon(aes(x = x,
y = y,
color = shape,
fill = shape,
group = triangle)) +
coord_equal()
Unfortunately, the corners won’t match based on the order shapes are drawn.
color = NA
ggplot(tiles) +
geom_polygon(aes(x = x,
y = y,
fill = shape,
group = triangle),
color = NA) +
coord_equal()
Again, the lines still show.
lwd
ggplot(tiles) +
geom_polygon(aes(x = x,
y = y,
color = shape,
fill = shape,
group = triangle),
lwd = .5) +
coord_equal()
If lwd
is too large, the output looks messy. If lwd
is too small, the lines show. Unfortunately, there isn’t always an in-between option that fits.
ggplot(tiles) +
geom_polygon(aes(x = x,
y = y,
color = shape,
fill = shape,
group = triangle)) +
geom_polygon(aes(x = x,
y = y,
fill = shape,
group = triangle),
color = NA) +
coord_equal()
This option works sometimes and looks fine in this case.
The following output changed from png to jpeg.
ggplot(tiles) +
geom_polygon(aes(x = x,
y = y,
color = shape,
fill = shape,
group = triangle)) +
geom_polygon(aes(x = x,
y = y,
fill = shape,
group = triangle),
color = NA) +
coord_equal()
No lines, but there’s also a loss in resolution.
ggplot() +
geom_polygon(data = tiles[tiles$shape == 'T', ],
aes(x = x,
y = y,
color = shape,
fill = shape,
group = triangle)) +
geom_polygon(data = tiles[tiles$shape == 't', ],
aes(x = x,
y = y,
color = shape,
fill = shape,
group = triangle)) +
coord_equal()
Since this draws all of one shape on top, they display a little chunky. However, it could work if you have few colors and like that style.
The following code increases the polygons’ sizes in new variables, graphs the new larger shapes to cover up the lines, then places the original polygons on top to clean up the corners.
tiles_background <- tiles %>%
group_by(triangle) %>%
mutate(center_x = mean(x),
center_y = mean(y)) %>%
mutate(new_x = (x - center_x) + sign(x) * .075 + center_x,
new_y = (y - center_y) + sign(y) * .075 + center_y)
ggplot(data = tiles_background) +
geom_polygon(aes(x = new_x,
y = new_y,
fill = shape,
group = triangle)) +
geom_polygon(aes(x = x,
y = y,
fill = shape,
group = triangle)) +
coord_equal()
This one works out sometimes, but you’ll have to play around with the size increase.
ragg
packageThis code saves the image using the ragg
package with setting up background shapes.
library(ragg)
agg_png(filename = here::here("third_example", "img.png"),
width = 1500, height = 1500)
ggplot(data = tiles) +
geom_polygon(aes(x = x,
y = y,
color = shape,
fill = shape,
group = triangle)) +
geom_polygon(aes(x = x,
y = y,
fill = shape,
group = triangle),
color = NA) +
coord_equal()
invisible(dev.off())
This one tends to look great.