Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set plotly chart to have a transparent background in R?

Here's what I have, so far:

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)

I tried adding paper_bgcolor=#00000000, plot_bgcolor=#00000000 after the x and y axis information within layout(), but when I run the command, I get the plus sign. I don't know what to do, so any help is appreciated. Thanks!

like image 513
POW123 Avatar asked May 22 '16 22:05

POW123


People also ask

How to visualize transparent background in a plot in R?

To actually visualize a transparent background the image has to be stored as a PNG image, this can be done ggsave () function. In this approach, after the plot has been created normally, theme () function is added to it with rect parameter. To rect, element_rect () function is passed with parameter fill set to transparent.

How to export a ggplot2 plot with transparent rectangle elements?

Example 1 shows how to export a ggplot2 plot with transparent rectangle elements. Next, we can use the ggsave function to write our transparent plot to a PNG file: Have a look at your current working directory after running the previous R code. You should find a ggplot2 plot with transparent background.

Will a transparent background render black label text invisible?

Note that transparent backgrounds will also apply to exported images. Which can render black label text invisible in some image viewing applications, including Windows 10's default Photos tool.

What are traces and layout attributes in Plotly?

Plotly's graph description places attributes into two categories: traces(which describe a single series of data in a graph) and layoutattributes that apply to the rest of the chart, like the title, xaxis, or annotations).


2 Answers

Just try:

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'

You can change the rgb numbers to fit your needs.

like image 121
MLavoie Avatar answered Sep 20 '22 02:09

MLavoie


I find toy need to use rgba AND fig_bgcolor in layout

plt %>%
    layout(plot_bgcolor  = "rgba(0, 0, 0, 0)",
           paper_bgcolor = "rgba(0, 0, 0, 0)",
           fig_bgcolor   = "rgba(0, 0, 0, 0)")
like image 24
Benbob Avatar answered Sep 18 '22 02:09

Benbob