Home > Back-end >  Plotly and ggplot2
Plotly and ggplot2

Time:05-11

I am working with Plotly library. Below you can see the code for producing Treemap.

library(plotly)
library(dplyr)

dt=data.frame(
          types= rep("stories",10),
          kind= c('kind_1','kind_2','kind_3','kind_1','kind_2','kind_3','kind_1','kind_2','kind_3','kind_1'),
          values=seq(1:10))

Treemap<-plot_ly(data = dt,
                 type= "treemap",
                 values= ~values,
                 labels= ~kind,
                 parents=  ~types,
                 domain= list(column=0),
                 name = " ",
                 textinfo="label value percent parent")%>%
    layout(title="Treemap")

Treemap

So the above code shows us Treemap with a title. This title has different font in comparison with plots from the ggplot2 library. Below you can see one example of a chart in ggplot2 with some title

library(ggplot2)

qplot(values, kind, data = dt)  ggtitle("Treemap")

So can anybody help me how to change the title in Treemap with Plotly library and to have a title in the same font as the title in ggplot2?

CodePudding user response:

So the default font for ggplot is Arial/Helvetica. You can change the font in plotly by adding font in your layout. You can use the following code:

library(plotly)
t <- list(
  family = "Arial")

Treemap<-plot_ly(data = dt,
                 type= "treemap",
                 values= ~values,
                 labels= ~kind,
                 parents=  ~types,
                 domain= list(column=0),
                 name = " ",
                 textinfo="label value percent parent")%>%
  layout(title="Treemap", font = t)

Treemap

Output:

enter image description here

You can change the font to whatever you want if available.

Comment: Add bold to title

To add a bold title you can use html tags like this:

Treemap<-plot_ly(data = dt,
                 type= "treemap",
                 values= ~values,
                 labels= ~kind,
                 parents=  ~types,
                 domain= list(column=0),
                 name = " ",
                 textinfo="label value percent parent")%>%
  layout(title="<b>Treemap</b>", font = t)

Treemap

Output:

enter image description here

  • Related