2

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

图片: ggplot 和 ggplotly 图表之间的区别

这是我提到的代码: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)

谢谢

4

1 回答 1

1

看起来您偶然发现了一个(或两个)错误(也许您应该在githubggplotly上提出问题)。

  1. 第一个问题是数据集中的“间隙”在通过ggplotly.

  2. 第二个问题是ggplotly无法转换由guides(fill=guide_legend(title='Legend')).

作为解决方法

  1. 第一个问题,您可以扩展数据集以包含 和 的所有X.Axis组合。Y.AxisGroup
  2. 第二个问题,您可以删除分箱颜色条并将其替换为连续色阶。

不完美,但通过这种方式转换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)

在此处输入图像描述

于 2020-06-17T14:21:34.087 回答