ggplotly - 使用色彩美学时不匹配

时间:2017-10-16 10:36:50

标签: r ggplot2 plotly ggplotly

我正在使用ggplotly将ggplot对象转换为交互式图形。我遇到的问题是当ggplot对象使用相关的颜色美学时。 例如,使用以下虚拟示例:

library(ggplot2)
library(plotly)

datetime.data = seq.POSIXt(from = ISOdatetime(2017, 10, 1, 0, 0, 0), to = ISOdate(2017, 10, 15, 23, 0, 0), by = 'hour')
date.data <- as.Date(datetime.data)
x.data <- as.numeric(strftime(datetime.data, format="%H")) * 60 + as.numeric(strftime(datetime.data, format="%M"))
y.data1 <- as.numeric(date.data) + sin(x.data)
color.data <- runif(length(x.data), 0, 10)

df <- data.frame(Datetime = datetime.data, Date = date.data, X = x.data, Y = y.data1, Color = color.data)

# continuous color scale
gg <- ggplot(df, aes(x=X, y=Y, group=Date)) + geom_line(aes(color=Color)) + ggtitle("Continuous")
print(gg)

ggly <- ggplotly(gg)
print(ggly)

# discrete color scale
df[, "Color"] <- round(df[, "Color"])
df[, "Color"] <- as.factor(df[, "Color"])

gg <- ggplot(df, aes(x=X, y=Y, group=Date)) + geom_line(aes(color=Color)) + ggtitle("Discrete")
print(gg)

ggly <- ggplotly(gg)
print(ggly)

结果使用ggplot和相应的plotly对象:

ggplot - continuous color scale

ggplotly - continuous color scale

ggplot - discrete color scale

ggplotly - discrete color scale

有了这么基本的例子,我认为它可能与某些版本的bug有关?我正在使用更新的软件包,尽管在使用ggplotly()时建议使用ggplot [using devtools::install_github('hadley/ggplot2')]的开发版本。我的包和R版本:

> packageVersion("ggplot2")
 ‘2.2.1.9000’

> packageVersion("plotly")
 ‘4.7.1’

> R.version$version.string            
" R version 3.4.0 (2017-04-21) "

0 个答案:

没有答案