[英]ggplotly in r generates different legend from ggplot
我在 R 中創建了以下 dataframe。 第一步是導入必要的庫
library(ggplot2)
library(plotly)
library(dplyr)
我們在這里創建dataframe如下
DF_1<-data.frame("A"= c(1:10))
DF_1$B<-c("D", "C")
DF_1$C<-DF_1$A^2
接下來我們創建一個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()
渲染 p2 時,圖例會正確顯示。 當我在 ggplotly 中嵌套 p2 時,圖例變為兩條黑線。
p3<-ggplotly(p2, dynamicTicks = T)
p3= layout(p3, xaxis = list(type = "log"))
是否可以在p3中保留p2的傳說? 我請人看看
看起來ggplotly
在如何設置美學方面比ggplot2
更明智。 只需將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.