Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting individual axis labels in bold using ggplot2

I want to highlight individual axis labels in bold. I am aware of this answer by @MrFlick but I can't figure out how to do this a) for more than one item, and b) whether it's possible to use the names of the labels instead of the number of the item in that list (or expression).

Here is an example dataset:

require(ggplot2)
require(dplyr)
set.seed(36)
xx<-data.frame(YEAR=rep(c("X","Y"), each=20),
               CLONE=rep(c("A","B","C","D","E"), each=4, 2),
               TREAT=rep(c("T1","T2","T3","C"), 10),
               VALUE=sample(c(1:10), 40, replace=T))

Then I am sorting my labels according to a particular factor combination which is then supposed to be maintained across multiple panels of a plot. See my previous question here.

clone_order <- xx %>% subset(TREAT == "C"  & YEAR == "X") %>%
  arrange(-VALUE) %>% select(CLONE) %>% unlist()    
xx <- xx %>% mutate(CLONE = factor(CLONE, levels = clone_order))

ggplot(xx, aes(x=CLONE, y=VALUE, fill=YEAR)) + 
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~TREAT)

enter image description here

Now I want to bold Clone A, B and E. I am sure this will work somehow but I cannot figure out how. Ideally, it would be great to know how to do this by a) using the number of the item in the list/expression, and b) by using the label, e.g. A, B and E.

like image 846
Stefan Avatar asked Sep 26 '16 03:09

Stefan


People also ask

How do I make axis labels bold in R?

Make Axis Title Text Bold Font with element_text() To make both x and y-axis's title text in bold font, we will use axis. title argument to theme() function with element_text(face=”bold”). Note now both x and y axis's title text are in bold font.

How do I make my Axis bold in ggplot2?

text argument to make both x and y-axis text bold using element_text() function. We can make the axis text font bold by using face=”bold” argument to element_text() function. Note now the both x and y-axis text are in bold font and more clearly visible than the default axis text.

How do I customize axis labels in R?

To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.


2 Answers

You can create a named vector of expressions (that turn text to bold) in scale_x_discrete and use parse=TRUE to evaluate the expressions:

ggplot(xx, aes(x=CLONE, y=VALUE, fill=YEAR)) + 
    geom_bar(stat="identity", position="dodge") +
    facet_wrap(~TREAT) +
    scale_x_discrete(labels=c("A"=expression(bold(A)), "C"=expression(bold(C)),
                              "E"=expression(bold(E)), parse=TRUE))

You can probably create the vector of expressions programmatically, rather than typing it out, but the way to do that is escaping me right now.

enter image description here

like image 121
eipi10 Avatar answered Oct 13 '22 01:10

eipi10


I'm not sure if you can map label characteristics by name, but it's definitely possible to do it by position with a call to theme:

ggplot(xx, aes(x=CLONE, y=VALUE, fill=YEAR)) + 
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~TREAT) +
  theme(axis.text.x = element_text(face = c('bold', 'bold', 'plain', 'plain', 'bold')))

Note that the listed font faces for axis.text.x are the same length as the labels of your x-axis (five elements). This produces:

enter image description here

like image 38
jdobres Avatar answered Oct 13 '22 00:10

jdobres