ggplot2中标签文本条目中的不同字体和大小

时间:2011-07-11 16:16:47

标签: r ggplot2

我正在构建在轴文本中有两行的图表。第一行包含组名称,第二行包含该组人口。我将我的轴标签构建为单个字符串,格式为“LINE1 \ n LINE2”。是否可以为LINE1和LINE2分配不同的字体和大小,即使它们包含在单个字符串中?我希望LINE1大而粗,而LINE2要小而且不受欢迎。

以下是一些示例代码:

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

我尝试了这个,但结果是一个大的粗体条目,还有一个小的,无条件的条目:

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

另一个可能的解决方案是创建单独的图表元素,但我不认为ggplot2有一个'子轴标签'元素可用...

非常感谢任何帮助。

干杯, 亚伦

3 个答案:

答案 0 :(得分:14)

我还认为我不能仅使用ggplot2功能来制作图表。

我会使用grid.textgrid.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

请尝试根据您的环境(例如子轴标签和字体大小的位置)调整代码。

答案 1 :(得分:9)

我在下面找到了另一个简单的解决方案:

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

答案 2 :(得分:2)

全部,

使用Triad的作弊这是我能够在这个问题上最接近解决方案。如果您有任何问题,请与我们联系:

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

干杯, 亚伦