2017-09-04 1 views
0

J'ai construit un objet ggplot2 avec des zones ombrées. Malheureusement, je ne peux pas obtenir ces zones ombrées à l'intrigue, lors de l'application de ggplotly à l'objet ggplot2.Zones ombrées non représentées dans l'objet d'intrigue


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)) 

L'objet ggplot2 (avec des zones ombragées) est inférieure à


Plot with shaded areas

+1

je suppose que ne plotly pas de support pour 'vous de geom_rect', voir [options disponibles = geom_abline(), geom_bar(), etc.] (https://plot.ly/ggplot2/ – parth

Répondre

2

Vous pouvez obtenir les zones ombragées dans plotly par geom_bar. Comme noté dans le commentaire ci-dessus, l'intrigue soutient cela.

# 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 

plotly chart

+0

J'ai accepté la réponse. J'ai une question connexe, [link] (https://stackoverflow.com/questions/46042358/shaded-area-does-not-show-up-in-plotly-when-using-dates) – Aex