[英]ggplotly in r generates different legend from ggplot
I have created the following dataframe in R.我在 R 中创建了以下 dataframe。 The first step is to import the necessary libraries
第一步是导入必要的库
library(ggplot2)
library(plotly)
library(dplyr)
We create the dataframe here as follows我们在这里创建dataframe如下
DF_1<-data.frame("A"= c(1:10))
DF_1$B<-c("D", "C")
DF_1$C<-DF_1$A^2
Next we create a plot as follows接下来我们创建一个plot如下
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()
When p2 is rendered, the legend displays correctly.渲染 p2 时,图例会正确显示。 When I nest p2 in ggplotly, the legend changes to two dark lines.
当我在 ggplotly 中嵌套 p2 时,图例变为两条黑线。
p3<-ggplotly(p2, dynamicTicks = T)
p3= layout(p3, xaxis = list(type = "log"))
Is it possible to retain the legends of p2 in p3.是否可以在p3中保留p2的传说? I request someone to take a look
我请人看看
Looks like ggplotly
is a more sensible than ggplot2
in how one sets the aesthetics.看起来
ggplotly
在如何设置美学方面比ggplot2
更明智。 Simply moving the fill
aes from the global setup in ggplot
into geom_ribbon
gives the correct legend:只需将
fill
aes 从 ggplot 中的全局设置移动到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"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.