Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use italics for facet labels in ggplot2?

Tags:

r

ggplot2

I am trying to use italics for facet names in ggplot2. I am first ordering and renaming the facets. I have come across a potential solution, but this requires labeller=label_parsed and I am using labeller=name_labeller to rename my facets.

In the following example, I would like the facet names "one" "two" "three" "four" and "five" to be italicized. Here is an example dataframe:

structure(list(Names = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 
5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), Other = c(5L, 
3L, 2L, 6L, 5L, 4L, 5L, 4L, 5L, 3L, 2L, 6L, 5L, 4L, 5L, 4L, 4L, 
5L, 3L, 2L), X = c(0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 
0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L), Y = c(0L, 10L, 
0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 0L, 10L, 
0L, 10L, 0L, 10L)), .Names = c("Names", "Other", "X", "Y"), class = "data.frame", row.names = c(NA, 
-20L))

and the code to produce the plot is

library(ggplot2)
library(grid)
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)



facets <- c("1", "2", "3", "4", "5")

names <- list(
  '1'="one",
  '2'="two",
  '3'="three",
  '4'="four",
  '5'="five"
)

name_labeller <- function(variable,value){
  return(names[value])
}

ggplot(FacetTestData[FacetTestData$Names %in% facets,], aes(y = Y, x = X, group = Names)) + 
  geom_point(shape = 21, size=3) + 
  scale_fill_manual(values=c("gray90","gray40")) + 
  geom_smooth(method="lm", se= FALSE, size = 1) +
  scale_color_manual(values=c("black","black")) +
  geom_smooth(method = 'lm', size = 1, colour = 'red', se = FALSE)  +
  facet_grid(Names ~ ., labeller=name_labeller)

The following code appears to give italics attributes to the list names

levels(names) <- c("italic('one')", "italic('two')", "italic('three')", "italic('four')",    "italic('five')")

but I can't quite figure out how to make this work with facet_grid(). Does anyone know how I can do this?

like image 676
Thomas Avatar asked Nov 17 '13 23:11

Thomas


People also ask

How do you change facet wrap labels in R?

Change the text of facet labels Facet labels can be modified using the option labeller , which should be a function. In the following R code, facets are labelled by combining the name of the grouping variable with group levels. The labeller function label_both is used.

How do I change the size of a facet label in ggplot2?

By default, the size of the label is given by the Facets, here it is 9. But we can change the size. For that, we use theme() function, which is used to customize the appearance of plot. We can change size of facet labels, using strip.

What is facet wrap in ggplot2?

facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as.

What does facet wrap mean in R?

Facet wraps are a useful way to view individual categories in their own graph. For example, if you wanted to make a separate graph for each cut measuring the price (y axis) for each clarity (x axis), you could add facet_wrap(~cut) .


2 Answers

Something along these lines should work:

... + theme(strip.text = element_text(face = "italic"))

See the docs for more detail about theme().

like image 114
krlmlr Avatar answered Sep 28 '22 08:09

krlmlr


You can separate the axes graphics using the code from krlmlr and modifying

... + theme(strip.text = ...   

to

... + theme(strip.text.x = ...    

OR

... + theme(strip.text.y = ...
like image 32
mcclur51e Avatar answered Sep 28 '22 08:09

mcclur51e