0

I used barplot3d::barplot3d to visualize my data. I have been trying to assign a color to each row instead of using topcolors = rainbow(100), but I could not figure it out. I appreciate any helps, suggestions, or references.

Here is the code that I have:

barplot3d(rows = 4,
          cols = 29,
          z = ECdata$EC50,
          scalexy = 0.5, 
          alpha = 0.2,
          theta = 50,
          phi = 50, 
          topcolors = rainbow(100),
          xlabels = 1:29,
          ylabels = c("Adepidyn", "Boscolid", "Fluopyram", "Solateno"), 
          xsub = "Isolate",
          ysub = "Fungicide",
          zsub = "EC50")
slamballais
  • 3,081
  • 3
  • 17
  • 29
  • [See here](https://stackoverflow.com/q/5963269/5325862) on making a reproducible example that is easier for folks to help with. – camille Jun 08 '21 at 02:47
  • `rainbow(100)` returns just a vector of 100 hex valued colors in rainbow order. You can make your own vector of hex colors like `c("#000000","#FFFFFF",...)` and replace the topcolors argument with that –  Jun 08 '21 at 16:16

1 Answers1

0

Answer

Just repeat each column cols times, where cols is the number of columns:

rows <- 4
cols <- 5
my_colors <- rep(rainbow(rows), each = cols)
barplot3d(rows = rows, 
          cols = cols, 
          z = runif(rows * cols),
          topcolors = my_colors)

enter image description here


Rationale

With rainbow(100), you get color codes for 100 colors. We want the color codes to be the same for each row. So, each color code should be repeated as many times as we have columns.

Assume we want 5 columns. Let's store that as:

cols <- 5

Now, we can repeat each color code 5 times:

my_colors <- rep(rainbow(100), each = cols))
head(my_colors, 20)
#  [1] "#FF0000FF" "#FF0000FF" "#FF0000FF" "#FF0000FF" "#FF0000FF" "#80FF00FF" "#80FF00FF" "#80FF00FF" "#80FF00FF" "#80FF00FF" "#00FFFFFF" "#00FFFFFF"
# [13] "#00FFFFFF" "#00FFFFFF" "#00FFFFFF" "#8000FFFF" "#8000FFFF" "#8000FFFF" "#8000FFFF" "#8000FFFF"

However, if we only have 4 rows, all the colors in your barplot3d will be red-ish, since we're only getting the first 4 colors of the 100 colors:

enter image description here

Thus, we can just call rainbow with the number of rows, to get more varying colors:

my_colors <- rep(rainbow(rows), each = cols)
slamballais
  • 3,081
  • 3
  • 17
  • 29