ggpairs更改因子级别的colum标签

时间:2015-12-14 20:55:25

标签: r ggplot2

可以更改因子级别的列标签,而无需更改data.frame

中的值

例如在下图中我可以分别将女性和男性的标签更改为F和M而不必更改df吗?

library(GGally)
data(tips, package = "reshape")
pm <- ggpairs(tips, 1:3, columnLabels = c("Total Bill", "Tip", "Sex"))
pm

tips

1 个答案:

答案 0 :(得分:2)

之后

pm <- ggpairs(tips, 1:3, columnLabels = c("Total Bill", "Tip", "Sex"))

这样做

levels(pm$data$sex)[levels(pm$data$sex) == "Male"] = "M"
levels(pm$data$sex)[levels(pm$data$sex) == "Female"] = "F"

你会得到这个情节:

enter image description here

它不会改变tips数据集中的任何内容:

head(tips)

 total_bill  tip    sex smoker day   time size
1     16.99 1.01 Female     No Sun Dinner    2
2     10.34 1.66   Male     No Sun Dinner    3
3     21.01 3.50   Male     No Sun Dinner    3
4     23.68 3.31   Male     No Sun Dinner    2
5     24.59 3.61 Female     No Sun Dinner    4
6     25.29 4.71   Male     No Sun Dinner    4