ggplot geom-tile有问题,请使用ggplotly转换为plotly chart

问题描述 投票:2回答:1

我试图通过使用ggplotly函数将ggplot几何图块图表转换为plotly。但是,我意识到结果是不同的。请参考下面的链接以查看区别。除此之外,ggplotly图表还缺少颜色条。请帮忙。

图片:Difference between ggplot and ggplotly chart

这是我提到的代码:https://www.r-graph-gallery.com/285-waffer-map.html

代码:

madeUp=read.table("https://raw.githubusercontent.com/holtzy/R-graph-gallery/master/DATA/madeUp.csv", sep=",", header=T)


library(tidyverse)


theData <- madeUp %>% 
  group_by(X.Axis, Y.Axis, Group) %>% 
  dplyr::summarize(statistic=mean(randVals, na.rm = TRUE))

fig <- ggplot(theData, 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))+
  guides(fill=guide_legend(title='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)+
  scale_fill_gradientn(colors = rainbow(100))

#Convert to ggplotly chart
fig <- ggplotly(fig)

谢谢

r shiny plotly ggplotly geom-tile
1个回答
0
投票

    第一个问题是,通过ggplotly转换ggplot时,数据集中的“空白”丢失了。
  1. 第二个问题是ggplotly无法转换guides(fill=guide_legend(title='Legend'))添加的合并色条。

  2. 作为解决方法
  3. 第一个问题,您可以扩展数据集以包含X.AxisY.AxisGroup的所有组合。

    第二个问题,您可以删除装箱的颜色栏,并用连续的色标替换它。
  1. 并不完美,但是通过ggplotly进行的转换为您提供了正确的绘图和图例。试试这个:
  2. 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)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.