I'm looking to present the results of a user survey of semantic differential scores as a boxplot, with the first semantic displayed along the default x-axis, and the second semantic displayed along a secondary top x-axis. In simple terms this can be represented as
df <- data.frame(sem1 = c("good", "big", "red"), sem2 = c("bad", "small", "blue"), a = c(4,6,1), b = c(6,2,6), c = c(8,3,7))`
df %>% pivot_longer(!c(sem1, sem2), names_to = "user", values_to = "score") %>%
ggplot()
geom_boxplot(aes(x = sem1, y = score))
ylim(1,10)
Which produces the below chart. What I would really like to do is add the characters from sem2 (i.e. "bad", "small", "blue") to a secondary axis along the top of the graph. Everything I've seen using sec_axis seems geared towards transformations of numerical axes rather than the addition of a distinct second axis. I believe I could possibly use annotation_custom to add each semantic one by one, but in my actual application with ~15 semantic pairs to display this could get quite cumbersome. Is there a more elegant way?
CodePudding user response:
A possible option:
library(tidyverse)
df <- data.frame(sem1 = c("good", "big", "red"),
sem2 = c("bad", "small", "blue"),
a = c(4, 6, 1), b = c(6, 2, 6),
c = c(8, 3, 7)) %>%
pivot_longer(!c(sem1, sem2), names_to = "user", values_to = "score")
df %>%
ggplot(aes(sem1, score))
geom_boxplot()
geom_label(aes(y = max(score * 1.2), label = sem2))
ylim(1, 10)
Created on 2022-05-19 by the reprex package (v2.0.1)