Author

Tony Duan

Code
library(gapminder)
library(plotly)
packageVersion("plotly")
[1] '4.10.4'

1 Scatter Plot

Code
library(reshape2)
tips=tips
head(tips)
  total_bill  tip    sex smoker day   time size
1      16.99 1.01 Female     No Sun Dinner    2
2      10.34 1.66   Male     No Sun Dinner    3
3      21.01 3.50   Male     No Sun Dinner    3
4      23.68 3.31   Male     No Sun Dinner    2
5      24.59 3.61 Female     No Sun Dinner    4
6      25.29 4.71   Male     No Sun Dinner    4
Code
fig <- plot_ly(data = tips, x = ~total_bill, y = ~tip)
fig

1.1 color by group

Code
fig <- plot_ly(data = tips, x = ~total_bill, y = ~tip,color=~sex)
fig

1.2 size by group

Code
fig <- plot_ly(data = tips, x = ~total_bill, y = ~tip,color=~sex,size=~size)
fig

1.3 line Plot

Code
data001=gapminder
data002= data001 %>% group_by(continent,year) %>% summarise(pop=sum(pop))
Code
fig <- plot_ly(data = data002 %>%filter(continent=='Asia'), x = ~year, y = ~pop,mode = 'lines')
fig

1.4 line width and colour

Code
fig <- plot_ly(data = data002 %>%filter(continent=='Asia'), x = ~year, y = ~pop,mode = 'lines',line = list(color = 'rgb(205, 12, 24)', width = 8))
fig

1.5 color by group

Code
fig <- plot_ly(data = data002, x = ~year, y = ~pop,color = ~continent,mode = 'lines')
fig

2 histogram

Code
fig <- plot_ly(data=tips,x = ~total_bill, type = "histogram")

fig

2.1 set bin number

Code
fig <- plot_ly(data=tips,x = ~total_bill, type = "histogram",nbinsx = 5 )
fig

2.2 color by group

Code
fig <- plot_ly(data=tips,x = ~total_bill,color=~sex, type = "histogram")

fig

3 bar chart

Code
tips2=tips %>% group_by(sex) %>% summarise(total_bill=sum(total_bill))
Code
fig <- plot_ly(data=tips2,x = ~sex,y=~total_bill,color=~sex, type = "bar")

fig

3.1 show number

Code
fig <- plot_ly(data=tips2,x = ~sex,y=~total_bill,text=~total_bill, type = "bar")

fig

3.2 Horizontal Barplot

Code
fig <- plot_ly(data=tips2,y = ~sex,x=~total_bill,color=~sex, type = "bar",orientation = 'h')

fig

3.3 bar chart order

Code
fig <- plot_ly(data=tips2,x = ~sex,y=~total_bill,color=~sex, type = "bar")%>% 
  layout(xaxis = list(categoryorder = "total ascending"))

fig
Code
fig <- plot_ly(data=tips2,x = ~sex,y=~total_bill,color=~sex, type = "bar")%>% 
  layout(xaxis = list(categoryorder = "total descending"))

fig

4 box plot

Code
fig <- plot_ly(data=tips,y=~total_bill, type = "box")

fig

4.1 color by group

Code
fig <- plot_ly(data=tips,x = ~sex,y=~total_bill,color=~sex, type = "box")

fig

5 strip plot

create ggplot and then covert to plotly

Code
p=ggplot(tips, aes(day,tip)) + geom_jitter(width = 0.1)
ggplotly(p)

5.1 color by group

Code
p=ggplot(tips, aes(day,tip,color=sex)) + geom_jitter(position=position_jitterdodge())
ggplotly(p)

6 Facet plot

create ggplot and then covert to plotly

Code
p=ggplot(tips, aes(tip,total_bill,)) + geom_point(aes(color=sex)) + facet_wrap("day")

ggplotly(p)

7 title,size,x y name,footnote

7.1 add title

Code
fig <- plot_ly(data=tips,x = ~total_bill,color=~sex, type = "histogram") %>% layout(title = 'new title')

fig

7.2 adjust size

Code
fig <- plot_ly(data=tips,x = ~total_bill,color=~sex, type = "histogram"
               ,width = 500, height = 200)

fig

7.3 change x y name

Code
fig <- plot_ly(data=tips,x = ~total_bill,color=~sex, type = "histogram") %>% layout(title = 'new title'
                                                                                   ,xaxis = list(title = 'new x')
                                                                                  ,yaxis = list(title = 'new y') 
                                                                                    )

fig

7.4 change x y axis range

Code
fig <- plot_ly(data=tips,x = ~total_bill,color=~sex, type = "histogram") %>% layout(title = 'new title'
                                                                                   ,xaxis = list(title = 'new x',range=c(20,40))
                                                                                  ,yaxis = list(title = 'new y',range=c(20,40)) 
                                                                                    )

fig

7.5 add footnote

Code
fig%>% layout(annotations = 
                         list(x = 0, y = -0.1, 
                              text = "this is footnote", 
                              showarrow = F, 
                              xref='paper', 
                              yref='paper')
    )

7.6 legend

7.6.1 hide legend

Code
fig <- plot_ly(data = data002, x = ~year, y = ~pop,color = ~continent,mode = 'lines') %>% layout(showlegend = FALSE)
fig

7.6.2 show legend and change legend name

Code
fig <- plot_ly(data = data002 %>% filter(continent=='Asia'), x = ~year, y = ~pop,color = ~continent,mode = 'lines',name='Asia pop') %>% layout(showlegend = TRUE)
fig

7.6.3 change legend and change legend Position

Code
fig <- plot_ly(data = data002, x = ~year, y = ~pop,color = ~continent,mode = 'lines') %>% layout(legend = list(orientation = 'h'))

fig

8 add image

add online image ::: {.cell}

Code
fig <- plot_ly(data = data002, x = ~year, y = ~pop,color = ~continent,mode = 'lines') %>% layout(
    images = list(  
      list(  
        source =  "https://raw.githubusercontent.com/cldougl/plot_images/add_r_img/vox.png"
        ,xref = "paper"
        ,yref = "paper" 
        ,x = 0.1
        ,y = 0.1
        ,sizex = 0.1 
        ,sizey = 0.1
       ,xanchor="left"  
      )  
    )
    ) 
                                                                                           
fig

:::

add local image ::: {.cell}

Code
fig <- plot_ly(data = data002, x = ~year, y = ~pop,color = ~continent,mode = 'lines') %>% layout(
    images = list(  
      list(  
        source = base64enc::dataURI(file = "bee.png")
        ,xref = "paper"
        ,yref = "paper" 
        ,x = 0.1
        ,y = 0.1
        ,sizex = 0.1 
        ,sizey = 0.1
       ,xanchor="left"  
       # image at the back
         ,layer = "below"
       
      )  
    )
    ) 
                                                                                           
fig

:::

9 applying themes

create ggplot and then covert to plotly

10 Save plot

install orca in https://github.com/plotly/orca#installation

conda install -c plotly plotly-orca ::: {.cell}

Code
#install.packages('processx')

:::

Code
#orca(p, "surface-plot.png")

11 Animation plot

Code
fig <- plot_ly(data=gapminder,x = ~gdpPercap,y=~lifeExp,color=~continent, type = "scatter",frame = ~year,text = ~country)

fig %>% animation_opts(
    1000, easing = "elastic", redraw = FALSE
  )

12 reference:

https://plotly.com/r/

Back to top