r - 在 ggpairs 图中更改标签
问题描述
我有以下代码:
library(GGally)
group <- c("b", "c", "d", "c", "d", "b", "d", "c", "b", "c", "d", "c", "d", "b", "d", "c")
one <- c(123, 174, 154, 143, 184, 134, 165, 192, 123, 174, 154, 143, 184, 134, 165, 192)
two <- c(223, 274, 254, 243, 284, 234, 265, 292, 223, 274, 254, 243, 284, 234, 265, 292)
three <- c(323, 374, 354, 343, 384, 334, 365, 392, 323, 374, 354, 343, 384, 334, 365, 392)
four <- c(423, 474, 454, 443, 484, 434, 465, 492, 423, 474, 454, 443, 484, 434, 465, 492)
df <- data.frame(group, one, two, three, four)
p <- df %>% ggpairs(.,
mapping = ggplot2::aes(colour=group),
lower = list(continuous = wrap("smooth")),
diag = list(continuous = wrap("blankDiag"))
)
getPlot(p, 1, 5) + guides(fill=FALSE)
plots = list()
for (i in 1:5){
plots <- c(plots, lapply(2:p$ncol, function(j) getPlot(p, i = i, j = j)))
}
p <- ggmatrix(plots,
nrow = 5,
ncol=p$ncol-1,
xAxisLabels = p$xAxisLabels[2:p$ncol],
yAxisLabels = p$yAxisLabels,
)
p
这是输出。 我希望这些组以不同的顺序显示。我可以通过替换数据框的组列中的字符串来做到这一点:
df$group = str_replace_all(df$group,"b","2-b")
df$group = str_replace_all(df$group,"c","1-c")
df$group = str_replace_all(df$group,"d","3-d")
这使事情按我想要的顺序排列,但标签变为“1-c”、“2-b”、“3-d”,因为我更改了一些值。是否可以在情节中分别用“c”、“b”、“d”替换“1-c”、“2-b”、“3-d”,同时保留新的顺序?还是有其他解决方案?
解决方案
推荐阅读
- angular - 为什么轮播不滑动或重定向到空白页面?
- reactjs - 需要传递相同组件的值
- jmeter - 我们如何使用脚本启用套接字测试 Jmeter 并运行测试用例?
- makefile - 字符串中的变量名不计算
- python-3.x - 查找段落中的平均单词数和句子数
- python - 为什么 K-means 聚类的分类精度会随着训练数据的增加而降低?
- unity3d - 如何在Unity中使人物面朝相反方向?
- javascript - 试图限制 ASP.NET MVC 中 int 数据类型的大小限制
- python - 如何将过滤后的查询集从列表视图传递到另一个使用 PDFTemplateView 进行打印的视图?
- javascript - V-FOR VUE 不显示图像