【问题标题】:R - Title not removed with plot.title = element.blank() using plotlyR - 使用 plotly 未使用 plot.title = element.blank() 删除标题
【发布时间】:2017-01-03 15:29:07
【问题描述】:

我与ggplot2plotly 合作。我必须允许用户选择标题的显示(显示或不显示)。

因此,我使用ggplot2 的主题功能,更准确地说是plot.title = element.blank()。它适用于ggplot2,但当我将绘图转换为绘图时,标题仍然存在。

解决问题的任何解决方案(除了删除labs() 功能)?也许是plotly_build() 的解决方法?

这是一个简化的例子。

# 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

【问题讨论】:

  • element_text(color="#00000000")(标题颜色为 0 alpha)。如果看起来更合适,您可以将其设为 element_text(color="#ffffff00")

标签: r plot ggplot2 plotly


【解决方案1】:

plotly_build() 肯定会在这里工作:

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

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 2021-08-09
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 2017-09-24
    • 2021-03-14
    相关资源
    最近更新 更多