R ggplot2 hexbin plot graphics bug?

Viewed 472

I always spot a handful of strange white horizontal dotted lines that seem to appear on the borders between vertically adjacent hexagons on every hexbin density plot that I create using ggplot2 in the R environment. They certainly do not belong there... This behavior (bug) is reproducible on several R installations on different computers with different MS Windows versions and different graphics drivers. I don't have a clue on how to get rid of these dotted lines. Below is a plot showing the problem, and the code that produced the plot. Is this a known problem? Any hint to overcome this? Thanks a bunch for any help!

Plot with bespoke bug

## Load libraries
library(ggplot2)
library(hexbin)

## Create some data
set.seed(222)
myx <- data.frame(x = 0.4 + rnorm(10000, mean = 0, sd = 0.1))
myy <- data.frame(y = 0.5 + rnorm(10000, mean = 0, sd = 0.1))
MyData <- data.frame(xvariable=myx$x*100, yvariable=myy$y*100)

## Prepare the plot window
dev.off(2)
windows.options(width=6, height=6)
op <- par(mfrow = c(1,1))
op <- par(oma = c(0,0,0,0) + 0.1,
          mar = c(5.1, 5.1, 4.1, 2.1))

## Creaate the plot
ggplot(MyData, aes(x=xvariable, y=yvariable) ) +
  geom_hex(bins=66) +
  xlim(0, 100) +
  ylim(0, 100) +
  ggtitle("My plot") +
  scale_fill_continuous(type = "viridis") +
  theme_bw() +
  theme(plot.title = element_text(color="black", size=17, face="bold"),
        axis.title.x = element_text(color="black", size=17, face="bold"),
        axis.title.y = element_text(color="black", size=17, face="bold"),
        axis.text=element_text(color="black", size=15, face="bold"),
        legend.title = element_text(color = "black", size = 15), 
        legend.text = element_text(color = "black", size=12),
        legend.position = c(0.9,0.2), legend.direction = "vertical")
2 Answers

Looks like if you save as pdf and then convert that to image, there will be no line.

enter image description here

I have this issue too, RStudio version 1.4.1717 "Juliet Rose" (df86b69e, 2021-05-24) for Windows. Looks like white lines going across the 'Plots' tab if I use geom_hex(). If I resize the output window by a pixel at a time (vertically) , the lines will disappear.

Maybe there's too many hex bins to begin with and the resolution causes gaps every so often.

before enter image description here

after enter image description here

Related