Search code examples
rggvis

R Hexadecimal colours do not work in ggvis


I have been fiddling around with ggvis and ran into this problem that I cannot use hexadecimal values for colours like regular R plots. Is this normal behaviour for ggvis or am I missing something?

library(ggvis)

# create data frame
df <- data.frame(var1=1:10,var2=5:14,var3=factor(c(rep("A",5),rep("B",5)))) 

# code without custom colours
df %>%
  ggvis(x = ~var1, y = ~var2,fill = ~var3) %>%
  layer_points(size := 30,fillOpacity := 0.5) 

#code with custom colours as text
df %>%
  ggvis(x = ~var1, y = ~var2,fill = ~var3) %>%
  layer_points(size := 30,fillOpacity := 0.5) %>%
  scale_nominal("fill",range=c("red","green"))

# code with custom colours as hexadecimal values
df %>%
  ggvis(x = ~var1, y = ~var2,fill = ~var3) %>%
  layer_points(size := 30,fillOpacity := 0.5) %>%
  scale_nominal("fill",range=c("#0000FFFF","#FF3300FF"))

Solution

  • The 8-character codes you're using include 2 extra characters at the end, which, as you mentioned, control transparency. While these may be recognized in base R, ggvis is expecting a traditional hex code, i.e. one with 6 characters. To get the hex codes you need, simply omit the last 2 characters of your current color codes. Transparency is controlled separately through parameters passed to the ggvis family of functions.