How to change facet labels in facet_wrap

I use ggplot and facet_wrap to get the required graphs. I have to add a few things to the labels of each face or variable or name of each face, just like we modify 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 is change the label of each face i, e D, E, F, G, H, I, J to something else.

How can i change this?

Adding

Sorry, I tried to break it, but it takes time, so I added it to github. You can upload the file and check the result. The problem is option 4 facet_wrap ... you can select switch 4.

I commented on the previous facet_wrap, which I used when the data integrity is in order, but if I change the facet wrapper, the graph behaves differently as well as the data.

Download data can be found in the "Download Data" folder.

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

+4
source share
2 answers

Based on what I know, facet_grid might be the best solution in this case. facet_grid can not only help you group graphs by one variable, but also two or even more, there is a labeller argument, which is designed to set up a 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] DEFGHIJ Levels: D < E < F < G < H < I < J 

enter image description here

However, as you can see, the graph is on the same line, and I donโ€™t think it can be easily divided into several lines, even if you enabled the as.table flag based on.

Do you think this will be possible if you add a new column for labeling? Then you can keep awesome 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

+5
source

Although this is a very old question, I would like to answer it because I learned one simple way.

This is a solution with facet_wrap () and without changing your data either.

 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))) 
+2
source

Source: https://habr.com/ru/post/1207354/


All Articles