R 使用ggplotly将ggplot geom分幅转换为plotly图表时出现问题

R 使用ggplotly将ggplot geom分幅转换为plotly图表时出现问题,r,shiny,plotly,ggplotly,geom-tile,R,Shiny,Plotly,Ggplotly,Geom Tile,我尝试使用ggplotly函数将ggplot geom平铺图转换为plotly。然而,我意识到结果是不同的。请参考下面的链接查看差异。除此之外,ggplotly图表也缺少颜色条。请帮忙 图片: 这就是我提到的代码: 代码: madeUp=read.table(“https://raw.githubusercontent.com/holtzy/R-graph-gallery/master/DATA/madeUp.csv,sep=“,”,页眉=T) 图书馆(tidyverse) 数据% 分组依据(

我尝试使用ggplotly函数将ggplot geom平铺图转换为plotly。然而,我意识到结果是不同的。请参考下面的链接查看差异。除此之外,ggplotly图表也缺少颜色条。请帮忙

图片:

这就是我提到的代码:

代码:

madeUp=read.table(“https://raw.githubusercontent.com/holtzy/R-graph-gallery/master/DATA/madeUp.csv,sep=“,”,页眉=T)
图书馆(tidyverse)
数据%
分组依据(X轴、Y轴、分组)%>%
dplyr::汇总(统计=平均值(随机数,na.rm=真))

图看起来您在
ggplotly
中被一个(或两个)错误绊倒了(也许您应该在上提出问题)

  • 第一个问题是,通过
    ggplotly
    转换ggplot时,数据集中的“间隙”丢失

  • 第二个问题是
    ggplotly
    无法转换
    guides(fill=guides\u legend(title='legend'))
    添加的装箱色条

  • 作为解决方案

  • 第一个问题是,您可以扩展数据集以包括
    X.Axis
    Y.Axis
    组的所有组合
  • 第二个问题是,您可以移除装箱的颜色条,并用连续的颜色比例替换它
  • 虽然不完美,但通过
    ggplotly
    的转换可以为您提供正确的绘图和图例。试试这个:

    madeUp=read.table("https://raw.githubusercontent.com/holtzy/R-graph-gallery/master/DATA/madeUp.csv", sep=",", header=T)
    
    theData <- madeUp %>% 
      group_by(X.Axis, Y.Axis, Group) %>% 
      dplyr::summarize(statistic=mean(randVals, na.rm = TRUE)) %>% 
      ungroup()
    
    # Expand the Dataset to includ all Combinations of X.Axis, Y.Axis and Group
    theData1 <- tidyr::expand_grid(X.Axis = 0:49, Y.Axis = 0:30, Group = LETTERS[1:6]) %>% 
      dplyr::left_join(theData)
    
    fig <- ggplot(theData1, aes(X.Axis, Y.Axis)) +
      coord_cartesian(xlim = c(0,20), ylim = c(0,20)) +
      scale_x_continuous(breaks = seq(0,20)) +
      scale_y_continuous(breaks = seq(0,20))+
      geom_tile(aes(fill=statistic))+
      # Remove the binned colorbar
      # guides(fill=guide_legend(title='Legend'))+
      labs(fill = "Legend") +
      theme(
        panel.background = element_rect(fill= 'white', color = 'white'),
        panel.grid.major = element_line(color='#E0E0E0'),
        panel.grid.minor = element_line(color='#E0E0E0')
      )+
    
      ggtitle('Wafer Map')+
      facet_wrap(~Group)+
      # in case of ggplot2: Set the fill color for NA to "transparent"
      scale_fill_gradientn(colors = rainbow(100), na.value = "transparent")
    fig
    
    ggplotly(fig)
    
    madeUp=read.table(“https://raw.githubusercontent.com/holtzy/R-graph-gallery/master/DATA/madeUp.csv,sep=“,”,页眉=T)
    数据%
    分组依据(X轴、Y轴、分组)%>%
    dplyr::汇总(统计=平均值(随机数,na.rm=真))%>%
    解组()
    #展开数据集以包含X轴、Y轴和组的所有组合
    数据1%
    dplyr::左联合(数据)
    
    fig对于任何对填补空白感兴趣的人来说,有一个。
    madeUp=read.table("https://raw.githubusercontent.com/holtzy/R-graph-gallery/master/DATA/madeUp.csv", sep=",", header=T)
    
    theData <- madeUp %>% 
      group_by(X.Axis, Y.Axis, Group) %>% 
      dplyr::summarize(statistic=mean(randVals, na.rm = TRUE)) %>% 
      ungroup()
    
    # Expand the Dataset to includ all Combinations of X.Axis, Y.Axis and Group
    theData1 <- tidyr::expand_grid(X.Axis = 0:49, Y.Axis = 0:30, Group = LETTERS[1:6]) %>% 
      dplyr::left_join(theData)
    
    fig <- ggplot(theData1, aes(X.Axis, Y.Axis)) +
      coord_cartesian(xlim = c(0,20), ylim = c(0,20)) +
      scale_x_continuous(breaks = seq(0,20)) +
      scale_y_continuous(breaks = seq(0,20))+
      geom_tile(aes(fill=statistic))+
      # Remove the binned colorbar
      # guides(fill=guide_legend(title='Legend'))+
      labs(fill = "Legend") +
      theme(
        panel.background = element_rect(fill= 'white', color = 'white'),
        panel.grid.major = element_line(color='#E0E0E0'),
        panel.grid.minor = element_line(color='#E0E0E0')
      )+
    
      ggtitle('Wafer Map')+
      facet_wrap(~Group)+
      # in case of ggplot2: Set the fill color for NA to "transparent"
      scale_fill_gradientn(colors = rainbow(100), na.value = "transparent")
    fig
    
    ggplotly(fig)