本文介绍了将ggplot2::facet_WRAP标题从默认值更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何可能的方法来更改facet_WRAP变量的标签,如下所示。例如,我希望它不是cyl: 4
、cyl: 6
、cyl: 8
,而是condition: 4
、condition: 6
、condition: 8
。当然,我可以通过重命名变量来做到这一点,但这不是我想要的。这是一个简单得多的自定义函数版本,我不能随心所欲地重命名变量。
facet_wrap
贴上我喜欢的标签?有点像ggplot2
中的x
美学变量在数据帧(mtcars
)中可以有一些名称(例如cyl
),但我仍然可以使用labs(x = "cylinder")
将其替换为我自己的名称)。我希望facet_wrap
有类似的东西。library(dplyr)
library(datasets)
library(ggplot2)
data(mtcars)
# creating a dataframe
df <- dplyr::group_by(mtcars, .dots = c('cyl', 'am')) %>%
dplyr::summarize(counts = n()) %>%
dplyr::mutate(perc = (counts / sum(counts)) * 100) %>%
dplyr::arrange(desc(perc))
# preparing the plot
ggplot2::ggplot(df, aes('', counts)) +
geom_col(
position = 'fill',
color = 'black',
width = 1,
aes(fill = factor(am))
) +
facet_wrap(~cyl, labeller = "label_both") + # faceting by `cyl` variable
geom_label(
aes(label = paste0(round(perc), "%"), group = factor(am)),
position = position_fill(vjust = 0.5),
color = 'black',
size = 5,
show.legend = FALSE
) +
coord_polar(theta = "y")
由reprex package(v0.2.0)创建于2018-02-19。
推荐答案
要更改面标签,您可以向facet_wrap
中的labeller
参数提供命名的标签矢量:
labeller = labeller(cyl =
c("4" = "condition: 4",
"6" = "condition: 6",
"8" = "condition: 8"))
这里是完整的绘图代码:
ggplot2::ggplot(df, aes('', counts)) +
geom_col(
position = 'fill',
color = 'black',
width = 1,
aes(fill = factor(am))
) +
facet_wrap(~cyl, labeller = labeller(cyl =
c("4" = "condition: 4",
"6" = "condition: 6",
"8" = "condition: 8")
))
geom_label(
aes(label = paste0(round(perc), "%"), group = factor(am)),
position = position_fill(vjust = 0.5),
color = 'black',
size = 5,
show.legend = FALSE
) +
coord_polar(theta = "y")
根据请求返回标签的函数的注释进行编辑:
可能是这样的:
label_facet <- function(original_var, custom_name){
lev <- levels(as.factor(original_var))
lab <- paste0(custom_name, ": ", lev)
names(lab) <- lev
return(lab)
}
ggplot2::ggplot(df, aes('', counts)) +
geom_col(
position = 'fill',
color = 'black',
width = 1,
aes(fill = factor(am))
) +
facet_wrap(~cyl, labeller = labeller(cyl = label_facet(df$cyl, "grouping"))) +
geom_label(
aes(label = paste0(round(perc), "%"), group = factor(am)),
position = position_fill(vjust = 0.5),
color = 'black',
size = 5,
show.legend = FALSE
) +
coord_polar(theta = "y")
sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] bindrcpp_0.2 ggplot2_2.2.1 dplyr_0.7.4 RMOA_1.0 rJava_0.9-9 RMOAjars_1.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.14 bindr_0.1 magrittr_1.5 munsell_0.4.3 colorspace_1.3-2 R6_2.2.2 rlang_0.1.4
[8] plyr_1.8.4 tools_3.4.2 grid_3.4.2 gtable_0.2.0 yaml_2.1.14 lazyeval_0.2.1 assertthat_0.2.0
[15] digest_0.6.13 tibble_1.4.1 glue_1.2.0 labeling_0.3 compiler_3.4.2 pillar_1.0.1 scales_0.5.0.9000
[22] pkgconfig_2.0.1
这篇关于将ggplot2::facet_WRAP标题从默认值更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!