RMS Bplot / 3-D plot - change colour scheme

Dear all,
I want to change the default colour-scheme of a rms-bplot.
Do you know if that is possible? So far, I did not find a solution

set.seed(1234)
df <- data.frame(
  sex=factor(rep(c("0", "1"), each=200)),
  weight=round(c(rnorm(200, mean=55, sd=5),
                 rnorm(200, mean=65, sd=5))),
  height=round(c(rnorm(200, mean=160, sd=10),
                rnorm(200, mean=170, sd=10)))
)
library(rms)

lm3  <- rms::Glm(sex ~ weight + height, data=df, family="binomial")

dd <-  rms::datadist(df)
options(datadist = "dd")

bplot(Predict(lm3, weight=seq(40,100,by=2), height=seq(150,200,by=5),  fun = function(x)1/(1+exp(-x))))

Thank you very much!

Dear all,
I have realized that this has to do with the "lattice" pre-set for the color-scheme.

You need to change it according to:

And then somehow adopt it with "trellis.par.set()".

However, it did not work so far.

Could anybody help?

Hi @Georg_Semmler,
Here are two palettes available from other packages. The paletteer package is very useful when trying to decide on a palette. Of course, there are many, many options; or you can make your own palette from scratch.

library(lattice)
library(rms)
#> Loading required package: Hmisc
#> Loading required package: survival
#> Loading required package: Formula
#> Loading required package: ggplot2
#> 
#> Attaching package: 'Hmisc'
#> The following objects are masked from 'package:base':
#> 
#>     format.pval, units
#> Loading required package: SparseM
#> 
#> Attaching package: 'SparseM'
#> The following object is masked from 'package:base':
#> 
#>     backsolve
library(RColorBrewer)
library(paletteer)
library(ggplot2)

# help("paletteer")
# as.data.frame(palettes_c_names)

my_colours <- paletteer_c("ggthemes::Classic Green", 16)

set.seed(1234)
df <- data.frame(
  sex=factor(rep(c("0", "1"), each=200)),
  weight=round(c(rnorm(200, mean=55, sd=5),
                 rnorm(200, mean=65, sd=5))),
  height=round(c(rnorm(200, mean=160, sd=10),
                rnorm(200, mean=170, sd=10)))
)

lm3  <- rms::Glm(sex ~ weight + height, data=df, family="binomial")

# Default
bplot(Predict(lm3, 
              weight=seq(40,100,by=2), 
              height=seq(150,200,by=5),  
              fun = function(x)1/(1+exp(-x))))

# Discrete palette from RColorBrewer (but has too few colors for scale required)
bplot(Predict(lm3, 
              weight=seq(40,100,by=2), 
              height=seq(150,200,by=5),  
              fun = function(x)1/(1+exp(-x))),
      par.settings = list(regions = list(col=brewer.pal(9,"Set1"))))

# Sequential palette from ggthemes with 16 colors
bplot(Predict(lm3, 
              weight=seq(40,100,by=2), 
              height=seq(150,200,by=5),  
              fun = function(x)1/(1+exp(-x))),
      par.settings = list(regions = list(col=my_colours)))

Created on 2021-09-13 by the reprex package (v2.0.1)

1 Like

Thank you so much!
That really helped.

Could you maybe help me with one last thing: If I want to change the first two and the last two colors of the palette (here we have 16), how can I do this manually? Do I have to enter all 16 manually then?

Yours,
Georg

Hi @Georg_Semmler,
Its gets a bit messy since here we need the hex codes for the new colours, but this works

my_colours <- paletteer_c("ggthemes::Classic Green", 16)

# To change first colour to "white" via its hexadecimal code
my_colours[1] <- paste0("#", paste0(as.hexmode(as.numeric(col2rgb("white"))), collapse=""))

# To change last colour to "yellow" via its hexadecimal code
my_colours[16] <- paste0("#", paste0(as.hexmode(as.numeric(col2rgb("yellow"))), collapse=""))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.