Controlling the Overall Appearance of a Graph

This post shows how to control the overall appearance of graphics made by ggplot2.

 

Setting the Title of a Graph

If you have created a graph using the ggplot2 package then you must want to give a title to you graphs in this case you set the tile with the ggtitle() function.
CODE

p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()  #Creates a scatter plot and stores it in the variable p.
p #Displays the graph in the plots area in RStudio or Graphics window in RGUI.
p + ggtitle("Length and Width of the Sepal in Iris Data Frame") #ggtitle() is used to assign title to the Graph
p + ggtitle("Length and Width of the Sepal\n Iris Data Frame") #For a new line in the graph title. 

ggtitle() is equivalent to using labs(title = “Title text”) . The above generates the graph below.

91

Changing the Appearance of Text

You want to change the text appearance in the plot.

CODE
# Base plot
p <- ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()
# Controlling appearance of theme items
p + theme(axis.title.x=element_text(size=16, lineheight=.9, family=”Times”,
face=”bold.italic”, colour=”red”))
p + ggtitle(“Age and Height\nof Schoolchildren”) +
theme(plot.title=element_text(size=rel(1.5), lineheight=.9, family=”Times”,
face=”bold.italic”, colour=”red”))
# rel(1.5) means that the font will be 1.5 times the base font size of the theme.
# For theme elements, font size is in points.

 

To set the appearance of text geoms (text that’s in the plot itself, with geom_text() or annotate()), set the text properties.

p + annotate(“text”, x=15, y=53, label=”Some text”, size = 7, family=”Times”,
fontface=”bold.italic”, colour=”red”)
p + geom_text(aes(label=weightLb), size=4, family=”Times”, colour=”red”)

 

9496

 

 

 

 

 

 

 

 

 

 

 

 

9593

 

Changing the Appearance of Theme Elements

If you want to change the appearance of the To modify a theme, add theme() with a corresponding element_xx object. These include element_line, element_rect, and element_text. The following code shows how to modify many of the commonly used theme properties.

 

CODE

97

 

 

 

library(gcookbook) # For the data set
# Base plot
p <- ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex)) + geom_point()
# Options for the plotting area
p + theme(
panel.grid.major = element_line(colour=”red”),
panel.grid.minor = element_line(colour=”red”, linetype=”dashed”, size=0.2),
panel.background = element_rect(fill=”lightblue”),
panel.border = element_rect(colour=”blue”, fill=NA, size=2))

 

 

 

 

 

98

 

 

 

# Options for text items
p + ggtitle(“Plot title here”) +
theme(
axis.title.x = element_text(colour=”red”, size=14),
axis.text.x = element_text(colour=”blue”),
axis.title.y = element_text(colour=”red”, size=14, angle = 90),
axis.text.y = element_text(colour=”blue”),
plot.title = element_text(colour=”red”, size=20, face=”bold”))

 

 

 

 

 

 

99

 

 

# Options for the legend
p + theme(
legend.background = element_rect(fill=”grey85″, colour=”red”, size=1),
legend.title = element_text(colour=”blue”, face=”bold”, size=14),
legend.text = element_text(colour=”red”),
legend.key = element_rect(colour=”blue”, size=0.25))

 

 

 

 

 

 

 

911

 

 

 

# Options for facets
p + facet_grid(sex ~ .) + theme(
strip.background = element_rect(fill=”pink”),
strip.text.y = element_text(size=14, angle=-90, face=”bold”))
# strip.text.x is the same, but for horizontal facets

 

 

 

 

 

 

 

 

Creating Your Own Themes

If you want to create your own theme then you can do it by adding the customized elements to an existing theme.
Start with theme_bw() and modify a few things:
mytheme <- theme_bw() + theme(text = element_text(colour=”red”), axis.title = element_text(size = rel(1.25)))
# Base plot
p <- ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()

p + mytheme             # Plot with modified theme

 

912

Here you can see that the text of the axis colour is changed to red as assigned to the theme.

 

Hiding Grid Lines

The major grid lines are those that align with the tick marks, are controlled with panel.grid.major. The minor grid lines (the ones between the major lines) are controlled
with panel.grid.minor. This will hide them both.

 

library(gcookbook) # For the data set
p <- ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()
p + theme(panel.grid.major = element_blank(),      # this will hide the major ticks
panel.grid.minor = element_blank())                         # this will hide the minor ticks

We can use this alternatively to hide either of the grid lines so as to see either of the lines.

 

913914915

 

In the above figure,   you can see the in figure 1 there is no grid lines shown. In figure 2 the vertical grid lines are invisible and in the figure 3 the horizontal grid lines are invisible.

You can use the following code for the 2nd and 3rd figure.

CODE

# Hide the vertical grid lines (which intersect with the x-axis)
p + theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())
# Hide the horizontal grid lines (which intersect with the y-axis)
p + theme(panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank())

 

 

Leave A Reply

Your email address will not be published. Required fields are marked *