Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different font faces and sizes within label text entries in ggplot2

Tags:

r

ggplot2

I am building charts that have two lines in the axis text. The first line contains the group name, the second line contains that group population. I build my axis labels as a single character string with the format "LINE1 \n LINE2". Is it possible to assign different font faces and sizes to LINE1 and LINE2, even though they are contained within a single character string? I would like LINE1 to be large and bolded, and LINE2 to be small and unbolded.

Here's some sample code:

Treatment <- rep(c('T','C'),each=2)
Gender <- rep(c('Male','Female'),2)
Response <- sample(1:100,4)
test_df <- data.frame(Treatment, Gender, Response)

xbreaks <- levels(test_df$Gender)
xlabels <- paste(xbreaks,'\n',c('POP1','POP2'))

hist <- ggplot(test_df, aes(x=Gender, y=Response, fill=Treatment, stat="identity"))
hist + geom_bar(position = "dodge") + scale_y_continuous(limits = c(0, 
    100), name = "") + scale_x_discrete(labels=xlabels, breaks = xbreaks) + 
    opts(
      axis.text.x = theme_text(face='bold',size=12)
      )

I tried this, but the result was one large, bolded entry, and one small, unbolded entry:

hist + geom_bar(position = "dodge") + scale_y_continuous(limits = c(0, 
     100), name = "") + scale_x_discrete(labels=xlabels, breaks = xbreaks) + 
     opts(
      axis.text.x = theme_text(face=c('bold','plain'),size=c('15','10'))
     )

Another possible solution is to create separate chart elements, but I don't think that ggplot2 has a 'sub-axis label' element available...

Any help would be very much appreciated.

Cheers, Aaron

like image 504
aaron Avatar asked Jul 11 '11 16:07

aaron


People also ask

How do I change the font size of labels in R?

Go to the menu in RStudio and click on Tools and then Global Options. Select the Appearance tab on the left. Again buried in the middle of things is the font size. Change this to 14 or 16 to start with and see what it looks like.

How do I increase the size of a label in ggplot2?

To increase the X-axis labels font size using ggplot2, we can use axis. text. x argument of theme function where we can define the text size for axis element. This might be required when we want viewers to critically examine the X-axis labels and especially in situations when we change the scale for X-axis.

What font does ggplot2 use?

Show activity on this post. I think it is actually "sans" which defaults to Arial on many systems, but this really depends on the operating system and settings, on Linux ggplot will retrieve the default sans from fontconfig I guess (which will return Bitstream Vera Sans or Deja Vu Sans on a typical Linux system).

How do I change the font size in a Boxplot in R?

The font size of the main title of boxplot can be changed by defining the font size value using par(cex. main=”size”), here size value can be changed based on our requirement. This needs to be done before creating the boxplot, otherwise, there will be no effect on the size of the main title.


1 Answers

I also think that I could not to make the graph by only using ggplot2 features.

I would use grid.text and grid.gedit.

require(ggplot2)
Treatment <- rep(c('T','C'), each=2)
Gender <- rep(c('Male','Female'), 2)
Response <- sample(1:100, 4)
test_df <- data.frame(Treatment, Gender, Response)

xbreaks <- levels(test_df$Gender)
xlabels <- paste(xbreaks,'\n',c('',''))

hist <- ggplot(test_df, aes(x=Gender, y=Response, fill=Treatment,
  stat="identity"))
hist + geom_bar(position = "dodge") + 
  scale_y_continuous(limits = c(0, 100), name = "") +
  scale_x_discrete(labels=xlabels, breaks = xbreaks) + 
  opts(axis.text.x = theme_text(face='bold', size=12))
grid.text(label="POP1", x = 0.29, y = 0.06)
grid.text(label="POP2", x = 0.645, y = 0.06)
grid.gedit("GRID.text", gp=gpar(fontsize=8))

Different font faces and sizes within label text entries in ggplot2

Please try to tune a code upon according to your environment (e.g. the position of sub-axis labels and the fontsize).

like image 85
Triad sou. Avatar answered Oct 09 '22 15:10

Triad sou.