更改饼图中的背景颜色

Change the background color in pie chart

我可以通过将 plot_bgcolor='rgb(0,0,0)' 放在 layout 中来更改绘图条形图中的背景颜色,例如:

library(plotly)

fig <- plot_ly(
  x = c("giraffes", "orangutans", "monkeys"),
  y = c(20, 14, 23),
  name = "SF Zoo",
  type = "bar"
)

fig %>% layout(uniformtext=list(minsize=8, mode='hide'),plot_bgcolor='rgb(0,0,0)')

但同样的命令不适用于 plotly 饼图!

  USPersonalExpenditure <- data.frame("Categorie"=rownames(USPersonalExpenditure), USPersonalExpenditure)
  data <- USPersonalExpenditure[,c('Categorie', 'X1960')]

  fig <- plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie')
  fig <- fig %>% layout(title = 'United States Personal Expenditures by Categories in 1960',
                        xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                        yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                        plot_bgcolor='rgb(0,0,0)')

  fig

还是白底!

可以通过添加:

  USPersonalExpenditure <- data.frame("Categorie"=rownames(USPersonalExpenditure), USPersonalExpenditure)
  data <- USPersonalExpenditure[,c('Categorie', 'X1960')]

  fig <- plot_ly(data, labels = ~Categorie, values = ~X1960, type = 'pie')
  fig <- fig %>% layout(title = 'United States Personal Expenditures by Categories in 1960',
                        xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                        yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                        paper_bgcolor='rgba(0,0,0,1)')

  fig