如何在R中设置绘图图表具有透明背景?

问题描述 投票:0回答:3

到目前为止,这是我所拥有的:

f1 <- list(
   family = "Arial, sans-serif",
   size = 25,
   color = "white"
)
f2 <- list(
   family = "Old Standard TT, serif",
   size = 14,
   color = "black"

)
a <- list(
   title = "SALES PER SONG",
   titlefont = f1,
   showgrid = FALSE,
   showticklabels = TRUE,
   showline=TRUE,
   tickangle = 45,
   tickfont = f2
  )

 salesplot <-plot_ly(producersales, type="scatter", x=Producer, y=SalesPerSong, color=SongRange, colors=cols, mode="markers", size=SalesPerSong) %>% 
layout(xaxis = a, yaxis = a)

我尝试在

paper_bgcolor=#00000000, plot_bgcolor=#00000000
内的 x 和 y 轴信息后面添加
layout()
,但是当我运行命令时,我得到了加号。我不知道该怎么办,所以非常感谢任何帮助。谢谢!

r colors plotly r-plotly
3个回答
33
投票

尝试一下:

salesplot <-plot_ly(producersales, type="scatter", x=Producer, y=SalesPerSong, color=SongRange, colors=cols, mode="markers", size=SalesPerSong) %>% 
layout(xaxis = a, yaxis = a) %>% 
layout(plot_bgcolor='rgb(254, 247, 234)') %>% 
layout(paper_bgcolor='rgb(254, 247, 234)') #will also accept paper_bgcolor='black' or paper_bgcolor='transparent'

您可以更改 RGB 数字以满足您的需求。


4
投票

我发现玩具需要在布局中使用rgba和fig_bgcolor

plt %>%
    layout(plot_bgcolor  = "rgba(0, 0, 0, 0)",
           paper_bgcolor = "rgba(0, 0, 0, 0)",
           fig_bgcolor   = "rgba(0, 0, 0, 0)")

0
投票

虽然上述答案仍然有效,但plotly现在接受“透明”作为透明度的有效输入,例如:

plt %>%
    layout(plot_bgcolor  = "transparent",
           paper_bgcolor = "transparent",)
© www.soinside.com 2019 - 2024. All rights reserved.