Modifying the axes titles’ appearance
The plot that has been plotted using the ggplot2 package can also be beautified and customized further in order to increase the readability and in order to make it more presentable. For example, the titles of the axis can be aligned to a particular angle and given a particular color for example, in the following code snippet we are using the color red to denote the axis title for the x-axis and orange to denote the y-axis title respectively. We’ve also increased the size in order to enhance the readability of the axis title and the angles at which these texts are written have also been changed. This can be done using the theme method. In R, the theme method takes various arguments in order to set up the appearance of the specified parameter. The theme method has the following
Syntax : theme(axis.title = element_text(angle, size, color))
Arguments :
axis.title – By default the parameter axis.title is used to modify the appearance of both axes. If we specify axis.title.x, the appearance of the x axis is modified. Otherwise, if we specify axis.title.y the appearance of the y axis is modified.
angle – angle of placing the text at
size – size of the text
color – color of the axis titles’ text
R
#creating the plot #assigning groups based on col2 values #highlighting the rectangular based area ggplot (data_frame, aes (col1, col3, colour=col2)) + geom_point () + theme (axis.title.x = element_text ( angle = 90, size =20, color= "red" ))+ theme (axis.title.y = element_text ( angle = 270, size =20, color= "orange" )) |
Output
Working with Axes in R using ggplot2
The ggplot2 package is a powerful and widely used package for graphic visualization. It can be used to provide a lot of aesthetic mappings to the plotted graphs. This package is widely available in the R Programming Language. The package can be downloaded and installed into the working space using the following command.
install.packages("ggplot2")
The ggplot method can be used to create a ggplot object. The graphical object is used to create plots by providing the data and its respective points. The data can be plotted using both points as well as lines.
Syntax : ggplot(data, aes = )
Arguments :
data – The data to be plotted
aes – The aesthetic mappings
The geom_point method can be used to plot the data points on the existing visualization that has been created. The color has been assigned based on the grouping values assigned in the col2 of the data plane in the following code snippet.
That is since there are three groups a, b, and c three different colors are used to denote the values contained in these three groups. These parameters are specified in the aesthetic mappings, as discussed above in the ggplot method.
R
#installing the required libraries library ( "ggplot2" ) #creating a data frame data_frame = data.frame (col1 = c (1,4,2,5,6,9,5,3,6,3), col2 = c ( "a" , "b" , "a" , "c" , "b" , "b" , "b" , "a" , "c" , "a" ), col3 = c (3,2,4,2,1,4,8,6,4,2)) #creating the plot #assigning groups based on col2 values ggplot (data_frame, aes (col1, col3, colour=col2)) + geom_point () |
Output