What is the problem?

(I am using R 3.3.2, RStudio 1.0.44 and ggplot2 2.2.0 in this post).

I really like ggplot2 for making quality graphics in R. You can check my first post to see what can be done to improve 2D image representation. However, while developing the code that plots data in spherical geometry I run against a problem with polygon borders being shown even when I wanted to avoid it.

ggplot2’s geom_polygon command

In order to draw filled polygons in ggplot2 we have to provide a data frame that contains the polygon vertices and the values that are going to be used to fill it with colour. Let’s start with an example:

# include the ggplot2 package
library(ggplot2)

# declare the vectors containing the coordinates of the corners of four polygons.
x.bottom.left <- c(-1, -0.5, 0, 0.5)
x.bottom.right<- c( 0,  0.5, 1, 1.5)
x.top.left    <- c(-0.5, 0, 0.5, 1)
x.top.right   <- c( 0.5, 1, 1.5, 2)

y.bottom.left <- c(-1, 0, -1, 0)
y.bottom.right<- c(-1, 0, -1, 0)
y.top.left    <- c( 0, 1,  0, 1)
y.top.right   <- c( 0, 1,  0, 1)

# declare a vector of values
values <- c(1, 2, 3, 4)

# create a data frame containing values and ids
values.df <- data.frame(value = values, id = c(1, 2, 3, 4))

# create the x and y vectors containing the coordinates of each polygon in the correct order
x.coords <- unlist(lapply(1:length(x.bottom.left), function(i) {
    c(x.bottom.left[i], x.bottom.right[i], x.top.right[i], x.top.left[i])
}))

y.coords <- unlist(lapply(1:length(y.bottom.left), function(i) {
    c(y.bottom.left[i], y.bottom.right[i], y.top.right[i], y.top.left[i])
}))

# create a coordinates data frame
coords.df <- data.frame(id = rep(c(1, 2, 3, 4), each = 4), x = x.coords, y = y.coords)

# merge the data frames
data.df <- merge(values.df, coords.df, by = c("id"))

# plot
ggplot(data = data.df, aes(x = x, y = y)) + geom_polygon(aes(fill = value, group = id), color=NA) + theme_classic()

On my Mac there is an annoying white line around each polygon. It gets worse if the number of polygons increases. Let’s automate the coordinates creation routine and plot

# we want to plot a grid of 100 by 100 polygons
num.x <- 100
num.y <- 100

x.bottom.left <- rep(1e0, num.x * num.y)
y.bottom.left <- x.bottom.left

for (i in 1:num.x) {
    for (j in 1:num.y) {
 
        x.bottom.left[(i - 1) * num.y + j] <- -1 + 2 * (i - 1) / num.x + 1 * (j - 1) / num.y        
        y.bottom.left[(i - 1) * num.y + j] <- -1 + 2 * (j - 1) / num.y          
    }
}

x.bottom.right <- x.bottom.left + 2 / num.x
x.top.left <- x.bottom.left + 1 / num.y
x.top.right <- x.top.left + 2 / num.x
y.bottom.right <- y.bottom.left
y.top.left <- y.bottom.left + 2 / num.y
y.top.right <- y.bottom.right + 2 / num.y

# declare a vector of values
values <- seq(1:(num.x * num.y))

# create a data frame containing values and ids
values.df <- data.frame(value = values, id = seq(1:(num.x*num.y)))

# create the x and y vectors containing the coordinates of each polygon in the correct order
x.coords <- unlist(lapply(1:length(x.bottom.left), function(i) {
    c(x.bottom.left[i], x.bottom.right[i], x.top.right[i], x.top.left[i])
}))

y.coords <- unlist(lapply(1:length(y.bottom.left), function(i) {
    c(y.bottom.left[i], y.bottom.right[i], y.top.right[i], y.top.left[i])
}))

# create a coordinates data frame
coords.df <- data.frame(id = rep(seq(1:(num.x*num.y)), each = 4), x = x.coords, y = y.coords)

# merge the data frames
data.df <- merge(values.df, coords.df, by = c("id"))

# plot
ggplot(data = data.df, aes(x = x, y = y)) + geom_polygon(aes(fill = value, group = id), color=NA, linetype=0, lwd = 0) + theme_classic()