I am trying to create grouping x-axis labels to put above existing x-axis labels; recent browsing on StackOverflow recommends facet_grid for this purpose. However, I am unable to modify individual x-axis labels for each facet. Reprex below:
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")
I would also like SCALE1/SCALE2/etc. to be above A, B, C, etc., but that is likely a separate quesiton. I would appreciate any tips or insights.
CodePudding user response:
The following works for me. First, make the labels
vector with the name from your x-axis
label. Second, remove unique
from the 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")