Open In App

How to manipulate ggplot2 facet grid text size in R ?

Last Updated : 30 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be looking at the approach to change the size of the facet grid text size from the ggplot2 package in the R programming language.

We will use the theme() function, further with the strip.text.x = element_text(size) argument of this function, the user needs to enter the value of the size required as per the user demand, here the user have an option to either increase or decrease the size of the facet grid text size as the size value greater than 10 will be increasing the text size whereas the size value less than 10 will be decreasing the size of the facet grid text.

Let us first, plot the graph without any modification. So that, the difference is apparent.

Example:

R




library("ggplot2")   
  
gfg_data<-data.frame(x=c(1,2,3,4,5),y=c(5,4,3,2,1))
  
gfg_plot<-ggplot(data=gfg_data, aes(x, y)) + 
  geom_bar(stat="identity")+facet_grid(. ~ c('A','B','C','D','E'))
  
gfg_plot


Output:

Now lets use theme() function and create a graph with changed size of the facet grid text.

theme() function is a powerful way to customize the non-data components of your plots: i.e. titles, labels, fonts, background, gridlines, and legends. 

Syntax:

theme(line,rect,text,title,aspect.ratio,strip.text, …, validate = TRUE)

Parameters:

  • line: all line elements (element_line())
  • rect : all rectangular elements (element_rect())
  • text  :all text elements (element_text())
  • title : all title elements: plot, axes, legends (element_text(); inherits from text)
  • aspect.ratio  : aspect ratio of the panel
  • strip.text, strip.text.x, strip.text.y  : facet labels (element_text(); inherits from text). Horizontal facet labels (strip.text.x) & vertical facet labels (strip.text.y) inherit from strip.text or can be specified separately

Let us first increase the text size.

Example 1:

R




library("ggplot2")   
  
gfg_data<-data.frame(x=c(1,2,3,4,5),y=c(5,4,3,2,1))
  
gfg_plot<-ggplot(data=gfg_data, aes(x, y)) + 
geom_bar(stat="identity")+facet_grid(. ~ c('A','B','C','D','E'))+
theme(strip.text.x = element_text(size = 30))
  
gfg_plot


Output:

Let us now decrease the size. 

Example 2:

R




library("ggplot2"
  
gfg_data<-data.frame(x=c(1,2,3,4,5),y=c(5,4,3,2,1))
  
gfg_plot<-ggplot(data=gfg_data, aes(x, y)) + geom_bar(stat="identity")+
facet_grid(. ~ c('A','B','C','D','E'))+
theme(strip.text.x = element_text(size = 5))
  
gfg_plot


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads