Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the facet labels in facet_wrap

I am using ggplot and facet_wrap to get the required plots. I have to add few things to the labels of each facet or the variable or the name of each facet, just like how we modify the xlab and ylab directly under ggplot.

Example:

d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
  xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1)

d + facet_wrap(~ color)

enter image description here

All I want to do now is to change the label of each facet i,e D,E,F,G,H,I,J to something else.

How can I modify this?

Addition

Sorry I tried to break it but, it take time so I have added it in github. You can upload the file and check the result. The problem is with the option 4 facet_wrap...you can select the radio button option 4.

I have commented the previous facet_wrap I was using where the data integrity is fine, but if I change the facet wrap, the graph behaves differently and also the data.

Data to upload can be found in the folder "Data to upload"

Code can be found here: I will add this in a minute

like image 493
cppiscute Avatar asked Nov 20 '14 01:11

cppiscute


People also ask

How do you change a facet label?

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 you remove facet labels?

Facet labels Setting strip. text to element_blank() will remove all facet labels. You can also remove the labels across rows only with strip.

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

If we want to modify the font size of a ggplot2 facet grid, we can use a combination of the theme function and the strip. text. x argument. In the following R syntax, I'm increasing the text size to 30.

How do I reorder facets in R?

To reorder the facets accordingly of the given ggplot2 plot, the user needs to reorder the levels of our grouping variable accordingly with the help of the levels function and required parameter passed into it, further it will lead to the reordering of the facets accordingly in the R programming language.

How do I set facet labels?

Unlike with scales where you can set the labels, to set facet labels you must change the data values. Also, at the time of this writing, there is no way to show the name of the faceting variable as a header for the facets, so it can be useful to use descriptive facet labels.

How do you label a facet in R?

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. # Change facet text font.

Is it possible to show the name of the facet variable?

Also, at the time of this writing, there is no way to show the name of the faceting variable as a header for the facets, so it can be useful to use descriptive facet labels. With facet_grid () but not facet_wrap (), at this time), it’s possible to use a labeller function to set the labels.

How to change easily ggplot facet labels?

This article describes how to change easily ggplot facet labels. Load required packages and set the theme function theme_light () [ggplot2] as the default theme: Facet labels can be modified using the option labeller, which should be a function.


2 Answers

Though its a very old question , i would like to answer it for i learnt one easy method!!

This solution is with facet_wrap() and without changing your data in any manner also.

text.on.each.panel <-"_new"
d <- ggplot(diamonds, aes(carat, price)) +
     xlim(0, 2) 
d + facet_wrap(~ color, labeller = label_bquote(.(color)-.(text.on.each.panel)))
like image 179
joel.wilson Avatar answered Oct 03 '22 10:10

joel.wilson


Based on what I know, facet_grid might be a better solution in this case. facet_grid can not only help you group plots by one variable, but also two or even more, there is an argument called labeller which is designed to customize the label.

myfunction <- function(var, string) {
  print(var)
  print(string)
  result <- paste(as.character(string),'_new', sep="")
  return(result)
}

ggplot(diamonds, aes(carat, price, fill = ..density..)) + xlim(0, 2) + 
  stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1) + facet_grid(~color, labeller=myfunction, as.table=TRUE)

# OUTPUT
[1] "color"
[1] D E F G H I J
Levels: D < E < F < G < H < I < J

enter image description here

However, as you can see, the plot is in one row and I don't think it can be easily broken into multiple rows even if you turned on the as.table flag based on here.

Do you think it will be feasible if you add a new column dedicated for labelling? Then you can keep the awesomeness of facet_wrap...

diamonds$label <- paste(as.character(diamonds$color), "_new", sep="")
ggplot(diamonds, aes(carat, price, fill = ..density..)) + xlim(0, 2) + 
  stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1) + facet_wrap(~label)

enter image description here

like image 36
B.Mr.W. Avatar answered Oct 03 '22 09:10

B.Mr.W.