Various fonts and font sizes in text entries in ggplot2

I create charts that have two lines in the axis text. The first line contains the name of the group, the second line contains this group. I build my axis labels as a single character string with the format "LINE1 \ n LINE2". Is it possible to assign different faces and sizes of the LINE1 and LINE2 fonts, although they are contained in the same character string? I would like LINE1 to be large and bold, and LINE2 to be small and loose.

Here is a 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, bold record and one small, loose record:

 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 ggplot2 has an available sub-axis element ...

Any help would be greatly appreciated.

Cheers, Aaron

+13
r ggplot2
Jul 11 '11 at 16:16
source share
3 answers

I also think that I could not make a graph using only the ggplot2 functions.

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 customize the code to suit your environment (for example, the position of the sub-axis labels and the font).

+14
Jul 12 2018-11-11T00:
source share

I found another simple solution below:

 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[1] <- expression(atop(bold(Female), scriptstyle("POP1"))) xlabels[2] <- expression(atop(bold(Male), scriptstyle("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(label = xlabels, breaks = xbreaks) + opts( axis.text.x = theme_text(size = 12) ) 

another solution

+9
Jul 13 2018-11-11T00:
source share

All,

Using the Triad cheat, this is the closest I could find on this solution. Let me know if you have any questions:

 library(ggplot2) spacing <- 0 #We can adjust how much blank space we have beneath the chart here labels1= paste('Group',c('A','B','C','D')) labels2 = rep(paste(rep('\n',spacing),collapse=''),length(labels1)) labels <- paste(labels1,labels2) qplot(1:4,1:4, geom="blank") + scale_x_continuous(breaks=1:length(labels), labels=labels) + xlab("")+ opts(plot.margin = unit(c(1, 1, 3, 0.5), "lines"), axis.text.x = theme_text(face='bold', size=14)) xseq <- seq(0.15,0.9,length.out=length(labels)) #Assume for now that 0.15 and 0.9 are constant plot boundaries sample_df <- data.frame(group=rep(labels1,each=2),subgroup=rep(c('a','b'),4),pop=sample(1:10,8)) popLabs <- by(sample_df,sample_df$group,function(subData){ paste(paste(subData$subgroup,' [n = ', subData$pop,']',sep=''),collapse='\n') }) gridText <- paste("grid.text(label='\n",popLabs,"',x=",xseq,',y=0.1)',sep='') sapply(gridText, function(x){ #Evaluate parsed character string for each element of gridText eval(parse(text=x)) }) grid.gedit("GRID.text", gp=gpar(fontsize=12)) 

Cheers, Aaron

+1
Jul 26 '11 at 7:50
source share



All Articles