开发者

Different font faces and sizes within label text entries in 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


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).


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)
  )

Different font faces and sizes within label text entries in ggplot2


All,

Using Triad's cheat this is the closest I was able to get to solution on this one. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜