[英]Problem with ggplot geom-tile convert to plotly chartusing ggplotly
我嘗試使用 ggplotly function 將 ggplot geom-tile 圖表轉換為 plotly。 然而,我意識到結果是不同的。 請參閱下面的鏈接以查看差異。 除此之外,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)
謝謝
看起來您在 ggplotly 中偶然發現了一個(或兩個)錯誤(也許您應該在ggplotly
上提出問題)。
第一個問題是通過ggplotly
轉換 ggplot 時,數據集中的“間隙”會丟失。
第二個問題是ggplotly
無法轉換由guides(fill=guide_legend(title='Legend'))
添加的分箱顏色條。
作為解決方法
X.Axis
、 Y.Axis
和Group
的所有組合。 不完美,但通過ggplotly
進行的轉換可以為您提供正確的 plot 和圖例。 嘗試這個:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.