0

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I tried but again I can see differences. How I can put bold font because in the ggplot2 titles are in bold ?
@silent_hunter, I added code to the answer. You can use HTML tags to make your title bold.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.