R图,在3D热图上有不同的颜色和z轴

时间:2018-02-02 14:39:23

标签: r plotly heatmap r-plotly

我想用plotly绘制3D热图,但我希望颜色渐变与z轴无关。

使用ggplot2我可以生成以下图像:

dat <- data.frame("cat1" = sort(rep(seq(1:6), 6)),
                  "cat2" = rep(seq(1:6), 6),
                  "variable" = round(rnorm(36), 1),
                  "freq" = seq(100, (100-35)))

library(ggplot2)
ggplot(data.frame(dat), aes(cat1, cat2, fill = variable, label = freq)) + 
  geom_tile() +
  geom_text()

enter image description here

现在使用plotly,我无法将高度与一列相关(此处为freq),而将颜色与另一列(列variable)相关联。下图将高度和颜色与freq相关联。

library(plotly)
mat <- tapply(dat$freq, list(dat$cat1, dat$cat2), sum)
plot_ly(z = ~ mat) %>% add_surface

enter image description here

2 个答案:

答案 0 :(得分:1)

你在找这样的东西吗?

verbose_check

我认为plot_ly(z=~mat, type="surface", surfacecolor = matrix(nrow = 6, ncol = 6, rep(1:6, each = 6)) 可以得到你想要的东西。

答案 1 :(得分:0)

基于@MLavoie的建议,我找到了一种去相关3D dat <- data.frame("cat1" = sort(rep(seq(1:6), 6)), "cat2" = rep(seq(1:6), 6), "variable" = round(rnorm(36), 1), "freq" = seq(100, (100-35))) library(plotly) mat <- tapply(dat$freq, list(dat$cat1, dat$cat2), sum) mat_col <- tapply(dat$variable, list(dat$cat1, dat$cat2), sum) plot_ly(z = ~ mat, type = "surface", surfacecolor = mat_col) 的颜色和高度的方法。

 public class Bank
   {
   private BankAccount[] accounts; //Don't name variables with uppercase
   private int accountsPointer; //This is going to keep track of how many accounts are there in the array

   public Bank() //The constructor doesn't need to accept any bank account since it'll start as empty
   {
    accounts = new BankAccount[10]; //Here we initialize the array
    accountsPointer = 0; //Here the pointer starts as 0 since the array is empty
   }
   public void addAccount(BankAccount account){
       accountsPointer++;
       if(accountsPointer<10){ //We test it here so it won't throw an out of bounds exeption
           accounts[accountsPointer-1]=account;//It assigns the account to the next empty space on the array
       }
   }

enter image description here

相关问题