简体   繁体   中英

Legend and colours missing when using plotly from ggplot2

I have made a boxplot using ggplot2 which appears beautifully coloured in R, with the corresponding legend. When I export this to plotly, the boxplots are all red, and the legend has disappeared. I cannot post a picture of the plot on here, as it wont let me, but my code is as follows:

r <- ggplot(data=all.data, aes(factor(Year), Data))+xlab("Year")
r <- r + geom_boxplot(outlier.shape = NA, aes(fill = factor(Month))) 
r <- r + scale_y_continuous(limits = c(-1, 6.5))
r <- r+ scale_fill_discrete(name = "Month")
r
gg <- ggplotly(r,kwargs=list(world_readable=FALSE))

I have included within the geom_boxplot layer the fill command which deals with colour, but I cannot understand why this is not transferring.

Do you have the latest ggplot2 and plotly packages installed? I tried an easy bar chart but cannot reproduce your problem.

I have these versions installed:

packageVersion("ggplot2")
# [1] ‘1.0.1’
packageVersion("plotly")
# [1] ‘1.0.7’
packageVersion("shiny")
# [1] ‘0.12.2’

If your versions are older, you might consider upgrading to the latest greatest.

On my machine this silly example (similar to your incomplete code snippet):

library(plotly)
library(ggplot2)

all.data <- data.frame(Year = c(2000, 2000, 2005, 2005),
                       Month = c("Jan", "Jan", "Jul", "Jul"),
                       Data = c(0, 3.2, 3, 4))

r <- ggplot(data = all.data, aes(factor(Year), Data)) + xlab("Year")
r <- r + geom_boxplot(outlier.shape = NA, aes(fill = factor(Month))) 
r <- r + scale_y_continuous(limits = c(-1, 6.5))
r <- r + scale_fill_discrete(name = "Month")
r

Sys.setenv("plotly_username" = "MyCoooolUsername")
Sys.setenv("plotly_api_key" = "MySeCrEtPWD")
plotly:::verify("username")
plotly:::verify("api_key")
gg <- ggplotly(r,world_readable=TRUE)
gg

produces

在此处输入图片说明

as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM