Search code examples
rcolorsscatter3dplot3d

R: White plot elements on black background


Is there an easy way in R to make all elements of a plot (axes, axis labels, grid lines, tick marks...) colored white with a black background? I've seen some options for making a black background but I don't know how to recolor the plot elements.

I'm hoping to do this on a relatively complex plot. My plot is very similar to the scatter3D example here under "Change color by groups" using the iris dataset. I have included the necessary code to replicate that plot below.

library(plot3D)

# Set up data
data(iris)
x <- sep.l <- iris$Sepal.Length
y <- pet.l <- iris$Petal.Length
z <- sep.w <- iris$Sepal.Width

# Make 3d scatterplot with colors by category
scatter3D(x, y, z, bty = "g", pch = 18, 
      col.var = as.integer(iris$Species), 
      col = c("#1B9E77", "#D95F02", "#7570B3"),
      pch = 18, ticktype = "detailed",
      colkey = list(at = c(2, 3, 4), side = 1, 
      addlines = TRUE, length = 0.5, width = 0.5,
      labels = c("setosa", "versicolor", "virginica")) )

Solution

  • par(bg = 'black', fg = 'white') # set background to black, foreground white
    scatter3D(
        x,
        y,
        z,
        bty = "u", ## 'u' seemed to look better than 'g'
        pch = 18,
        col.var = as.integer(iris$Species),
        col = c("#1B9E77", "#D95F02", "#7570B3"),
        pch = 18,
        ticktype = "detailed",
        colkey = list(
            at = c(2, 3, 4),
            side = 1,
            addlines = TRUE,
            length = 0.5,
            width = 0.5,
            labels = c("setosa", "versicolor", "virginica"),
            col.axis = "white",
            col.clab = "white"
        ),
        col.axis = "white",
        col.panel = "black",
        col.grid = "white"
    )
    

    enter image description here

    I just went through the argument list in ?scatter3D and set most of the colors to white - you might be able to strip some of those settings away (or find more you want to add). For example, I didn't check if having col.axis = 'white' is necessary in both the main call and in the colkey list. There also may be more colors to set (like col.ticks). I'd recommend searching the help page for "col".