2017-01-03 5 views
1

Ich arbeite mit ggplot2 und plotly. Ich muss dem Benutzer erlauben, die Anzeige des Titels für ein Diagramm auszuwählen (angezeigt oder nicht).R - Titel nicht entfernt mit plot.title = element.blank() mit plotly

Daher verwende ich das Thema Merkmal ggplot2 und genauer plot.title = element.blank(). Es funktioniert mit ggplot2 aber der Titel ist immer noch da, wenn ich das Diagramm in einem Diagramm plotly umwandele.

Irgendeine Lösung, um das Problem zu beheben (außer die labs() Funktion zu entfernen)? Vielleicht ein Workaround mit plotly_build()?

Hier ist ein vereinfachtes Beispiel.

# Libraries and function 
library(ggplot2) 
library(plotly) 

CountPlotFunction <- function(MyData) 
{ 
    MyPlot <- ggplot(data = MyData, aes(x = MyData)) + 
    geom_bar(stat = "count", aes(fill = MyData)) + 
    scale_x_discrete(drop = FALSE) + 
    scale_fill_discrete(drop = FALSE) + 
    labs(title = "A title for my plot") + 
    ThemeByUser 
    return(MyPlot) 
} 

# Data 
CountryGroup <- c("Russia","Canada","Australia","Australia","Russia","Australia","Canada","Germany","Australia","Canada","Canada") 
df <- data.frame(CountryGroup) 

# Analysis 
ThemeByUser <- theme(plot.title = element_blank()) # or NULL if we want the title... 

ThePlot <- CountPlotFunction(MyData = df) 

print(ThePlot) # OK with ggplot2 
ggplotly(ThePlot) # NOK with plotly 
+0

'ELEMENT_TEXT (color = "# 00000000")' (0 alpha auf die Farbe des Titels). Sie können dafür sorgen, dass es 'element_text (color =" # ffffff00 ")' ist, wenn das passender erscheint. – hrbrmstr

Antwort

1

plotly_build() wird sicherlich hier arbeiten:

ThemeByUser <- theme(plot.title = element_blank()) # or NULL if we want the title... 

ThePlot <- CountPlotFunction(MyData = df) 

pb <- plotly_build(ThePlot) 

if(is.null(ThemeByUser)) { 
    pb$x$layout$title <- NULL 
} 

pb