【问题标题】:Shaded areas not shown in plotly object绘图对象中未显示的阴影区域
【发布时间】:2018-02-12 18:21:52
【问题描述】:

我已经构建了一个带有阴影区域的 ggplot2 对象。不幸的是,当将 ggplotly 应用于 ggplot2 对象时,我无法让这些阴影区域显示在 plotly 中。


library(ggplot2)
library(plotly)

#data
theDataFrame <- data.frame(game = c(1.0, 2.0, 1.0, 2.0), score = c(1:4), season = c("2000","2000","2001","2001"))
dataFrameForShadedAreas <- data.frame(theXmin = c(1.1, 1.5, 1.8), theXmax = c(1.2, 1.6, 1.9), dummyColors = c(4,7,9))

ggplotObj <- ggplot2::ggplot(data = theDataFrame, aes(x = game, y = score, color = season)) +
                ggplot2::geom_line() +
                ggplot2::geom_rect(data = dataFrameForShadedAreas, inherit.aes = FALSE,
                                  aes(xmin = theXmin, xmax = theXmax, ymin = -Inf, ymax = +Inf),
                                  #group = dummyColors),
                                  fill = 'turquoise3', alpha = 0.2)

(thePlotlyObj <- ggplotly(ggplotObj))

ggplot2 对象(带阴影区域)在下方


【问题讨论】:

标签: r plotly


【解决方案1】:

您可以通过geom_bar 将阴影区域设置为 plotly。正如上面评论中提到的那样,plotly 确实支持这一点。

# define theX as middle point between theXmin & theXmax
dataFrameForShadedAreas$theX = rowMeans(dataFrameForShadedAreas[,1:2])

ggplotObj <- ggplot(data = theDataFrame, aes(x = game, y = score, color = season)) +
  geom_line() +
  geom_bar(data = dataFrameForShadedAreas, inherit.aes = FALSE,
           aes(x = theX, y = 100), # y should be set to some height beyond the chart's range; y = Inf doesn't work
           stat = "identity", position = "stack", width = 0.1,
           fill = "turquoise3", alpha = 0.2) +
  coord_cartesian(ylim = c(1, 4)) # set limit for y-axis range here

【讨论】:

猜你喜欢
  • 2014-09-24
  • 1970-01-01
  • 2012-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多