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)
)
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
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
xlabels <- expression(paste(bold(Female), '\n', "POP1")) xlabels[2] <- expression(paste(bold(Male), '\n', "POP2"))
LucianoThe following code combines multiple lines of plotmath expressions; I
imagine it could be adapted to work with axis labels too.
library(gridExtra)
my.title = function(expressions) {
function(...)
tableGrob(expressions, parse=TRUE,
theme=theme.list(gpar.corefill = gpar(fill = NA, col = NA),
core.just = "center"))
}
e = expression(alpha,"testing", italic(italic),
hat(beta), integral(f(x)*dx, a, b))
qplot(1,1, geom="blank") + opts(axis.title.x = my.title(e))
HTH,
baptiste
In my original example expression() was actually unnecessary,
e = c('alpha',"testing", 'italic(italic)',
'hat(beta)', 'integral(f(x)*dx, a, b)')
qplot(1,1, geom="blank") + opts(axis.title.x = my.title(e))
works as well. You can construct a vector of strings with paste, e.g.
e = paste("alpha[", 1:3, "]",sep="")
qplot(1,1, geom="blank") + opts(axis.title.x = my.title(e))
This works fine for the x axis title. Axis labels, however, require
more work, and to be honest the solution is far too ugly to be useful
(this is because tableGrob is not vectorised for x and y). I post it
anyway, to justify the time I wasted on this :P
library(gridExtra)
breaks <- 1:4
## labels, to be split in 3 lines
labels <- paste("italic('first line') * alpha[", 1:4, "] * bold('last
line')",sep="")
my.labels <- function ()
{
structure(function(label, x, y) {
gTree(children=
do.call(gList,
mlply(data.frame(x=as.numeric(convertUnit(x, "native", "x")),
y=as.numeric(convertUnit(y, "native",
"y")),label=label),
function(x,y,label)
tableGrob(strsplit(label, "\\*")[[1]], parse=TRUE,
theme=theme.list(gpar.corefill = gpar(fill = NA,
col = NA),
core.just = "center"),
vp=viewport(unit(x,"native"),
y=unit(-2.5,"lines"), just="bottom")))))
}, class = "theme", type = "text", call = match.call())
}
qplot(1:4,1:4, geom="blank") +
scale_x_continuous(breaks=breaks, labels=labels) + xlab("")+
opts(axis.text.x = my.labels(), plot.margin = unit(c(1, 1, 3, 0.5), "lines"))
If I were you, I would look into tikzDevice instead; with some tweaks
of the plot margin, you could get a decent result with LaTeX
interpreting the labels.
Best,
baptiste