ggplot2中标签文本条目中不同的字体和大小 [英] Different font faces and sizes within label text entries in ggplot2

查看:794
本文介绍了ggplot2中标签文本条目中不同的字体和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下是一些示例代码:



治疗< - rep(c('T','C'),each = 2)
Gender < - rep(c('Male','女性'),2)
回应< - 样本(1:100,4)
test_df< - data.frame(治疗,性别,回应)

xbreaks< ; - 水平(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)

我试过了,但结果是一个大的粗体条目, ,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'))

另一个可能的解决方案是创建单独的图表元素,但我不认为ggplot2有一个'sub-axis label'元素可用...



任何帮助将非常感激。



干杯,
Aaron

解决方案

我不能仅通过使用ggplot2特性来创建图形。



我会使用 grid.text grid.gedit

  require(ggplot2)
Treatment< ; - rep(c('T','C'),each = 2)
Gender <-rep(c('Male','Female'),2)
响应<样本(1:100,4)
test_df< - data.frame(治疗,性别,反应)

xbreaks< - levels(test_df $ Gender)
xlabels< ; - paste(xbreaks,'\\\
',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))



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

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

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

这篇关于ggplot2中标签文本条目中不同的字体和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆