Alpha in ggplot geom_linerange determined by number of observations on Mac

  alpha, ggplot2, macos, r, windows

I am plotting some data using the geom_linerange function. This is daily observations over 5-10 years depending on the dataset.

When running the script on my Mac, the linerange alpha changes based on the number of observations in each plot. However, I want all plots to have alpha=1. Explicitly setting alpha within the geom_linerange function has no effect on the plot – the colours are still transparent when a large number of observations are plotted.

When I used the exact same script on my Windows laptop, the plot was correct with the default alpha of 1.

Below is a minimal working example:

library(ggplot2)
library(gridExtra)

df1 = data.frame(name = c("A","B","C"),
                Date = rep(seq(as.Date("2010-01-01"),as.Date("2018-01-01"),by=1),each=3),
                value = runif(8769,-1,1))

df2 = data.frame(name = c("A","B","C"),
                 Date = rep(seq(as.Date("2010-01-01"),as.Date("2014-01-01"),by=1),each=3),
                 value = runif(4386,-1,1))

df3 = data.frame(name = c("A","B","C"),
                 Date = rep(seq(as.Date("2010-01-01"),as.Date("2011-01-01"),by=1),each=3),
                 value = runif(1098,-1,1))

Plot1 = ggplot() +
  geom_linerange(data=df1,aes(x=name,ymin=Date,ymax=Date+1,colour=value),size=15) +
  scale_colour_gradient2(low="red",mid="white",high="blue",midpoint=0,name = "Value") +
  theme_bw() +
  coord_flip() + 
  xlab("Driver") +
  ylab("")

Plot2 = ggplot() +
  geom_linerange(data=df2,aes(x=name,ymin=Date,ymax=Date+1,colour=value),size=15) +
  scale_colour_gradient2(low="red",mid="white",high="blue",midpoint=0,name = "Value") +
  theme_bw() +
  coord_flip() + 
  xlab("Driver") +
  ylab("")

Plot3 = ggplot() +
  geom_linerange(data=df3,aes(x=name,ymin=Date,ymax=Date+1,colour=value),size=15) +
  scale_colour_gradient2(low="red",mid="white",high="blue",midpoint=0,name = "Value") +
  theme_bw() +
  coord_flip() + 
  xlab("Driver") +
  ylab("")


grid.arrange(Plot1,Plot2,Plot3)

Below is the output on my Mac. The top plot, with the most observations, has the lowest alpha:

Mac Alpha Plot

Below is the output on my Windows – as you can see, all plots have alpha=1:

Windows Alpha Plot

The code is transferred via GitHub repos.

Unfortunately, I am absolutely stumped as to why this is occurring. Is this expected behaviour on a Mac, or is there something I am doing wrong?

Many thanks!

Source: Windows Questions

LEAVE A COMMENT