如何在facet_wrap中更改构面标签 [英] How to change the facet labels in facet_wrap
问题描述
我正在使用ggplot和facet_wrap来获取所需的图.我必须在每个方面或变量或每个方面的名称的标签中添加一些内容,就像我们直接在ggplot下修改xlab和ylab一样.
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.
示例:
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1)
d + facet_wrap(~ color)
我现在要做的就是将D,E,F,G,H,I,J各个方面的标签更改为其他内容.
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.
我该如何修改?
添加
对不起,我试图打破它,但是,这需要时间,所以我将其添加到了github中. 您可以上传文件并检查结果. 问题在于选项4 facet_wrap ...您可以选择单选按钮选项4.
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.
我已经在数据完整性很好的地方使用了先前的facet_wrap,但是如果更改了facet包装,图形的行为也会与数据不同.
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
推荐答案
根据我的了解,在这种情况下facet_grid
可能是更好的解决方案. facet_grid
不仅可以帮助您按一个变量对图进行分组,还可以按两个或多个变量对图进行分组,其中有一个名为labeller
的自定义参数,用于自定义标签.
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
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.
如果添加专门用于标记的新列,您认为可行吗?然后,您可以保留facet_wrap
...
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)
这篇关于如何在facet_wrap中更改构面标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!