r - 带有 facet_grid 的多行 x 轴标签,其中每个方面都有唯一的标签
问题描述
我正在尝试创建分组 x 轴标签以放在现有 x 轴标签之上;最近在 StackOverflow 上的浏览建议为此使用 facet_grid。但是,我无法修改每个方面的单个 x 轴标签。下面的代表:
library(tidyverse)
# make data
scale <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o")
score <- rep(10, 15)
input <- data.frame(scale, score)
input <- t(input)
names(input) <- input[, 1]
input <- data.frame(janitor::row_to_names(input, row_number = 1))
# put it in a dataframe
df <- data.frame(rbind(
input$a, input$b, input$c, input$d,
input$e, input$f, input$g, input$h, input$i,
input$j, input$k, input$l,
input$m, input$n, input$o)) %>%
tibble::rownames_to_column(var = "scale") %>%
rename(scale_score = 2) %>%
mutate(scale_group = c("SCALE1", "SCALE1", "SCALE1", "SCALE1",
"SCALE2", "SCALE2", "SCALE2", "SCALE2", "SCALE2",
"SCALE3", "SCALE3", "SCALE3",
"SCALE4", "SCALE4", "SCALE4"))
df$scale_group = factor(df$scale_group, levels = c("SCALE1", "SCALE2", "SCALE3", "SCALE4"))
df$gp <- c(1,1,1,1,2,2,2,2,2,3,3,3,4,4,4)
# make ggplot labels
labels = c("A", "B", "C", "(D)",
"E", "F", "G", "(H)", "(I)",
"J", "K", "(L)",
"M", "N", "(O)")
# make a ggplot with facet_grid and it looks weird - why do the sub-labels repeat themselves?
df %>%
ggplot(aes(x=interaction(scale, factor(scale_group)), y=as.numeric(scale_score),
label = (scale_group),
group=interaction(scale_group, gp))) +
theme_bw() +
geom_line(stat="identity") +
geom_point(size=2, color="blue") +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = 9.5, ymax = 10.5),
alpha = 0.015, fill = "darkturquoise") +
scale_y_continuous(breaks = (seq(0, 20, by = 1)), limits = c(0,20)) +
scale_x_discrete(position = "top", labels = unique(labels)) +
xlab("") + ylab("") +
ggtitle("I want to see all the letters, not ABC repeating") + theme(plot.title = element_text(hjust = 0.5)) +
facet_grid( ~ scale_group,
scales = "free", space = "free")
我也想要 SCALE1/SCALE2/等。高于A、B、C 等,但这可能是一个单独的问题。我将不胜感激任何提示或见解。
解决方案
以下对我有用。首先,使用标签labels
中的名称制作矢量。x-axis
其次,unique
从labels = unique(labels)
.
# make ggplot labels
labels <- c("A", "B", "C", "(D)",
"E", "F", "G", "(H)", "(I)",
"J", "K", "(L)",
"M", "N", "(O)")
names(labels) <- interaction(df$scale, factor(df$scale_group))
df %>%
ggplot(aes(x=interaction(scale, factor(scale_group)), y=as.numeric(scale_score),
label = (scale_group),
group=interaction(scale_group, gp))) +
theme_bw() +
geom_line(stat="identity") +
geom_point(size=2, color="blue") +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = 9.5, ymax = 10.5),
alpha = 0.015, fill = "darkturquoise") +
scale_y_continuous(breaks = (seq(0, 20, by = 1)), limits = c(0,20)) +
scale_x_discrete(position = "top", labels = labels) +
xlab("") + ylab("") +
ggtitle("I want to see all the letters, not ABC repeating") + theme(plot.title = element_text(hjust = 0.5)) +
facet_grid( ~ scale_group,
scales = "free", space = "free")
推荐阅读
- django-rest-framework - 如何根据嵌套序列化程序中的数组获取主模型中的多条记录?
- git - 如何克隆远程 git 存储库,以便下载所有分支
- r - 在 R 中使用 ggplot2 在图中添加注释
- java - 比较两个不同的 MongoDB 集合 json 节点以进行验证
- pytorch - 在 PyTorch 中压缩两个连续维度
- c - C语言的注册和登录系统
- java - Java - 对象输入法(通过输入变量迭代)?
- python-3.x - 也许最好将所有模块文件名写在 /usr/local/lib/python3.9/urllib/__init__.py
- node.js - 套接字 IO 不适用于多个节点 js 服务器
- looker - Looker 如何检索半结构化数据中的某些值或相同值的重复值