Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

r - 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 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,'
',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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...