2

How to I order a set of variable names along the x-axis that contain letters and numbers? So these come from a survey where the variables are formatted like var1, below. But when plotted, they appear out_1, out_10, out_11...

But what I would like is for it to be plotted out_1, out_2...

library(tidyverse)
var1<-rep(paste0('out','_', seq(1,12,1)), 100)
var2<-rnorm(n=length(var1) ,mean=2)
df<-data.frame(var1, var2)
ggplot(df, aes(x=var1, y=var2))+geom_boxplot()

I tried this:

df %>% 
separate(var1, into=c('A', 'B'), sep='_') %>% 
arrange(B) %>%  
ggplot(., aes(x=B, y=var2))+geom_boxplot()
spindoctor
  • 1,719
  • 1
  • 18
  • 42
  • Possible duplicate of https://stackoverflow.com/q/3253641/3358272 – r2evans Jun 26 '18 at 17:16
  • 1
    That is, `ggplot2` is honoring the order of the levels within the factor `df$var1`. You have complete control over that, with `factor(..., levels=)` or `levels(...)<-`. – r2evans Jun 26 '18 at 17:17

1 Answers1

2

You can order the levels of var1 before plotting:

levels(df$var1) <- unique(df$var1)
ggplot(df, aes(var1,var2)) + geom_boxplot()

Or you can specify the order in ggplot scale options:

ggplot(df, aes(var1,var2)) +
  geom_boxplot() +
  scale_x_discrete(labels = unique(df$var1))

Both cases will give the same result:

enter image description here

You can also use it to give personalized labels; there's no need to create a new variable:

ggplot(df, aes(var1, var2)) +
  geom_boxplot() +
  scale_x_discrete('output', labels = gsub('out_', '', unique(df$var1)))

enter image description here

Check ?discrete_scale for details. You can use breaks and labels in different combinations, including the use of labels that came from outside your data.frame:

pers.labels <- paste('Output', 1:12)

ggplot(df, aes(var1, var2)) +
  geom_boxplot() +
  scale_x_discrete(NULL, labels = pers.labels)

enter image description here

  • This is better than mine. I'm not sure what I was thinking :) – De Novo Jun 26 '18 at 17:29
  • I should have made it clear; I need the labels to follow the order of another variable. I will change the code. – spindoctor Jun 26 '18 at 17:32
  • You don't need to create a new variable if it's just to personalize the labels. Just set what you want with the options `breaks` and `labels` inside scale_x. See `?discrete_scale` for details. – Carlos Eduardo Lagosta Jun 26 '18 at 18:28