r 中的 ggplotly 从 ggplot 生成不同的图例
ggplotly in r generates different legend from ggplot
我在 R 中创建了以下数据框。第一步是导入必要的库
library(ggplot2)
library(plotly)
library(dplyr)
我们在这里创建数据框如下
DF_1<-data.frame("A"= c(1:10))
DF_1$B<-c("D", "C")
DF_1$C<-DF_1$A^2
接下来我们创建如下图
p2<-ggplot(DF_1, aes(x=A, y=C, group=B, fill=B)) +
geom_line(size=.5) + geom_ribbon(data=subset(DF_1),aes(x=A,ymax=C),ymin=0,alpha=0.3) +
scale_fill_manual(name='Legend', values=c("green4", "red"), labels=c("D", "C" ))+theme_bw()
渲染 p2 时,图例显示正确。当我在 ggplotly 中嵌套 p2 时,图例变为两条黑线。
p3<-ggplotly(p2, dynamicTicks = T)
p3= layout(p3, xaxis = list(type = "log"))
是否可以在p3中保留p2的传说。我请人看一下
看起来ggplotly
比ggplot2
在审美设置上更明智。只需将 fill
aes 从 ggplot
中的全局设置移动到 geom_ribbon
即可给出正确的图例:
library(ggplot2)
library(plotly)
library(dplyr)
DF_1<-data.frame("A"= c(1:10))
DF_1$B<-c("D", "C")
DF_1$C<-DF_1$A^2
ggplot(DF_1, aes(x = A, y = C, group=B)) +
geom_line(size=.5) +
geom_ribbon(aes(x = A, ymin = 0, ymax = C, fill = B), alpha=0.3) +
scale_fill_manual(name='Legend', values=c("green4", "red"), labels=c("D", "C" ))+theme_bw()
ggplotly(dynamicTicks = T) %>%
layout(xaxis = list(type = "log"))
我在 R 中创建了以下数据框。第一步是导入必要的库
library(ggplot2)
library(plotly)
library(dplyr)
我们在这里创建数据框如下
DF_1<-data.frame("A"= c(1:10))
DF_1$B<-c("D", "C")
DF_1$C<-DF_1$A^2
接下来我们创建如下图
p2<-ggplot(DF_1, aes(x=A, y=C, group=B, fill=B)) +
geom_line(size=.5) + geom_ribbon(data=subset(DF_1),aes(x=A,ymax=C),ymin=0,alpha=0.3) +
scale_fill_manual(name='Legend', values=c("green4", "red"), labels=c("D", "C" ))+theme_bw()
渲染 p2 时,图例显示正确。当我在 ggplotly 中嵌套 p2 时,图例变为两条黑线。
p3<-ggplotly(p2, dynamicTicks = T)
p3= layout(p3, xaxis = list(type = "log"))
是否可以在p3中保留p2的传说。我请人看一下
看起来ggplotly
比ggplot2
在审美设置上更明智。只需将 fill
aes 从 ggplot
中的全局设置移动到 geom_ribbon
即可给出正确的图例:
library(ggplot2)
library(plotly)
library(dplyr)
DF_1<-data.frame("A"= c(1:10))
DF_1$B<-c("D", "C")
DF_1$C<-DF_1$A^2
ggplot(DF_1, aes(x = A, y = C, group=B)) +
geom_line(size=.5) +
geom_ribbon(aes(x = A, ymin = 0, ymax = C, fill = B), alpha=0.3) +
scale_fill_manual(name='Legend', values=c("green4", "red"), labels=c("D", "C" ))+theme_bw()
ggplotly(dynamicTicks = T) %>%
layout(xaxis = list(type = "log"))