Format Title of plotly Graph in R (Example)
But first, let us take a look at the steps involved in building a plotly graph in the R programming language:
Now that we know what steps are involved in formatting our plotly graph title, let’s get started!
Install R plotly Library
To build graphs with plotly in R, we must first install the plotly library. To do so, run the code below in your preferred code editor that can run R code. However, most R programmers prefer using the R Studio IDE for their R programming projects:
This will install plotly and all its dependencies in your environment.
Load R plotly Library
With the plotly library installed, let us now load it in our R environment by running the code below:
This code will load the plotly library and all the functions that are used to build graphs, including the one we will use in this tutorial.
Build A Histogram Plot
In this tutorial, we are going to build a simple histogram plot. To make the use of plotly easy and efficient in R, we will also need to install the dplyr library, which will enable us to use the pipe
|> operator to pipe lines of code together and run it as a chunk. To install dplyr, run this line of code:
install.packages("dplyr"). You can then load it like we did with plotly earlier by running
library(dplyr). We shall also need data to plot. Therefore, we are going to create a data frame to contain random data points as follows:
data <- data.frame( pointsA = rnorm(50), pointsB = rnorm(50))
You can print out the first 10 rows of the data frame by running
data |> head(10)
# pointsA pointsB #1 1.74614039 -0.457946421 #2 -0.16813711 1.102241491 #3 0.24815635 1.915307059 #4 -0.37187524 -0.101679168 #5 0.02387331 -2.433749817 #6 -0.27355968 -0.570405956 #7 0.65772890 -1.376176949 #8 1.37054114 -0.896023782 #9 -0.80669138 1.271767969 #10 -0.82824208 -0.730715849
Now that we have installed both plotly and dplyr and have also created our data frame, we can now build a histogram plot. In your code editor or R Studio IDE, run the code below to create a histogram plot:
fig <- data |> plot_ly(x = ~pointsA, type = "histogram") |> layout(title = "A Simple Histogram") print(fig)
Please note that the shape of your plot may be different from what we have in this tutorial because the data points are randomly generated.
Format Plot Title
With our histogram plot created, let us now format the title of the plot. We are going to change the font family to “Times New Roman” and set the font color to red. Please run the lines of code below:
format <- list( family = "Times New Roman", color = "red") fig <- data |> plot_ly(x = ~pointsA, type = "histogram") |> layout(title = list(text = "A Simple Histogram", font = format)) print(fig)
To format the plot title, we needed to create a format list, which comprised the font family and font color we used to format the title of our plot. You can play around with other font styles and colors as you please.
There you have it! It is very simple to format the title of your plotly graph in the R programming language. You can add other customizations to your plot to make it look the way you want.
I hope you found this tutorial helpful, and I will see you in the next one.
Video, Further Resources & Summary
Do you need more explanations on how to create interactive visualizations in plotly using the R programming language? Then you should take a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, Kirby White explains how to use the plotly library to create interactive visualizations in R.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you could have a look at some other tutorials on Statistics Globe:
- How to Draw a plotly Barplot in R (Example)
- Change plotly Axis Range in R (Example)
- How to Draw a plotly Line Plot in R (Example)
- How to Draw a plotly Scatterplot in R (Example)
- How to Draw a plotly Boxplot in R (Example)
- Introduction to R
This post has shown how to format the title of a plotly graph in the R programming language. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter