R plotly将ggplot2彩色矩形转换为灰色
我试着画一个包含矩形的图。我正在使用ggplot2创建它们,并希望通过将它们转换为plotly对象来“使它们具有交互性”。 现在的问题是,到plotly的转换似乎失去了ggplot2中指定的矩形颜色 下面是一个小的自我解释代码示例:R plotly将ggplot2彩色矩形转换为灰色,r,ggplot2,plotly,R,Ggplot2,Plotly,我试着画一个包含矩形的图。我正在使用ggplot2创建它们,并希望通过将它们转换为plotly对象来“使它们具有交互性”。 现在的问题是,到plotly的转换似乎失去了ggplot2中指定的矩形颜色 下面是一个小的自我解释代码示例: test.dat <- data.frame(xmin=c(0,1.5), ymin=c(-1,-1), xmax=c(1,2), ymax=c(1,1), col=c("blue", "red")) ggp.test <- ggplot() + geo
test.dat <- data.frame(xmin=c(0,1.5), ymin=c(-1,-1), xmax=c(1,2), ymax=c(1,1), col=c("blue", "red"))
ggp.test <- ggplot() + geom_rect(data=test.dat, aes(xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax), fill=test.dat$col) + theme_bw()
ggp.test
ply.test <- plotly_build(ggp.test)
ply.test
test.dat这与您指定颜色的方式有关。由于您直接添加fill
参数,而不是在aes
中,因此没有将矩形彼此分开的美学。ggplot似乎自动覆盖了这一点,但无法正确导出到plotly。当您将hovinf
添加为文本
aes
时,它可以使用这种美学来区分矩形,并能够赋予它们适当的颜色。添加另一种美学效果也可以使其发挥作用,例如使用组:
test.dat <- data.frame(xmin=c(0,1.5), ymin=c(-1,-1), xmax=c(1,2), ymax=c(1,1), col=c("blue", "red"))
ggp.test <- ggplot() + geom_rect(data=test.dat, aes(xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax, group = col), fill=test.dat$col) + theme_bw()
ggp.test
ply.test <- plotly_build(ggp.test)
ply.test
test.dat感谢您简单明了的解释!我现在知道问题出在哪里了。
test.dat <- data.frame(xmin=c(0,1.5), ymin=c(-1,-1), xmax=c(1,2), ymax=c(1,1), col=c("blue", "red"))
ggp.test <- ggplot() + geom_rect(data=test.dat, aes(xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax, group = col), fill=test.dat$col) + theme_bw()
ggp.test
ply.test <- plotly_build(ggp.test)
ply.test