Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R plotly将ggplot2彩色矩形转换为灰色_R_Ggplot2_Plotly - Fatal编程技术网

R 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

我试着画一个包含矩形的图。我正在使用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() + 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