Black Lives Matter. Please consider donating to Black Girls Code today.

[R] Layout of multiple figures + Bar charts colors function of quantitative data

Hi,

I try to make a heatmap with 2 bar charts corresponding to the sum of each line/column (code below) of the matrix. My result is not far away that I want but I have 3 questions about this:

  • Is there a better way to define the layout of multiple figures ?
  • Why do I have a point for the fucntion plotly_empty?
  • How can I have colors that are function of quantitative variable in bar chart? The “color” option give one color per bar as it was a qualitative variable. Do i just have to define a color palette according to data outside plot_ly function?

Thank you for your answers.

d <- diamonds[sample(nrow(diamonds), 1000), ]
aaa <- as.matrix(table(d$cut, d$clarity))
bbb <- as.data.frame(apply(aaa, 1, sum))
colnames(bbb) <- "cut"
bbb$cl <- rownames(bbb)
ccc <- as.data.frame(apply(aaa, 2, sum))
colnames(ccc) <- "clarity"
ccc$cl <- rownames(ccc)

p1 <- plot_ly(z = aaa, x = colnames(aaa), y = rownames(aaa), type = “heatmap”, colorbar = list(title = “”), showscale = FALSE)
p2 <- plot_ly(y = bbb$cl, x = bbb$cut, type = “bar”, orientation = “h”, color = bbb$cl, showlegend = FALSE)
p3 <- plot_ly(y = ccc$clarity, x = ccc$cl, type = “bar”, orientation = “v”, group = ccc$clarity, showlegend = FALSE)
pvid <- plotly_empty()

subplot(p3, pvid, p1, p2, nrows = 2) %>% layout(
yaxis = list(domain = c(0.90, 1), title = “”),
xaxis = list(domain = c(0, 0.85), title = “”, showticklabels = FALSE),
yaxis2 = list(domain = c(0.90, 1), title = “”),
xaxis2 = list(domain = c(0.90, 1), title = “”),
yaxis3 = list(domain = c(0, 0.85), title = “”),
xaxis3 = list(domain = c(0, 0.85), title = “”),
yaxis4 = list(domain = c(0, 0.85), title = “”, showticklabels = FALSE),
xaxis4 = list(domain = c(0.90, 1), title = “”)
)