我试图通过使用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)
谢谢
ggplotly
转换ggplot时,数据集中的“空白”丢失了。第二个问题是ggplotly
无法转换guides(fill=guide_legend(title='Legend'))
添加的合并色条。
第一个问题,您可以扩展数据集以包含X.Axis
,Y.Axis
和Group
的所有组合。
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)