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,161
  • 3
  • 18
  • 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,161
  • 3
  • 18
  • 29
  • Thank you so much for the help. It worked. One more question, Can I change the colors specifically. For example, black for row 1, red for row 2 and etc.? – Fereshteh Jun 08 '21 at 15:22
  • Yep, just replace `rainbow(rows)` with whatever you want. For example: `my_colors <- rep(c("black", "red", "blue", "green"), each = cols)` – slamballais Jun 08 '21 at 15:33
  • If this covers everything, can you accept the answer? This will indicate to others that the question has been resolved. – slamballais Jun 08 '21 at 15:34
  • Thank you so much for the help. Sorry I did not know that is needed. I accepted that! – Fereshteh Jun 09 '21 at 16:09