1

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")) )
Werner Hertzog
  • 2,002
  • 3
  • 24
  • 36
LCM
  • 292
  • 4
  • 14

1 Answers1

2
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".

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thanks much! I've read through the graphics options but can't figure out how to set the axis tick color and axis label color. Any ideas? In my plot I'm using `ticktype = "simple"` if that matters--generates arrows instead of ticks. – LCM Aug 11 '16 at 21:08
  • 1
    Ah, found it. `fg = 'white'` in `par`, or at least some of it. – Gregor Thomas Aug 11 '16 at 21:21
  • 1
    If you continue having trouble, you might try the `scatterplot3d` package, very similar functionality but perhaps a little more explicit in its documentation. The `par(bg = 'black', fg = 'white')` will get you most of the way there. – Gregor Thomas Aug 11 '16 at 21:24
  • 1
    Excellent! Just for anyone else who wanders in, `par(bg = 'black', fg = 'white'` changes everything except some of the colkey elements. `col.axis = "white", col.clab = "white"` still needs to be included in `colkey = list()`. – LCM Aug 11 '16 at 23:54