0

I have a dataset with multiple models of motorcycles concatenated with their cc's. When plotting these in ggplot with a corresponding value, they are out of order due to _ used in between the model and cc concatenation.

How can I order the x_axis on the graph by one column (an order column) and label from the model_cc concatenation column?

I have tried this to no avail:

ggplot - ordering x-axis labels by multiple columns

Here is some example code:

> library(tidyverse)
> 
> df <- data.frame(x_labels = c('A_1', 'A_100', 'A_2', 'A_200'),
> x_order=c(1, 3, 2, 4), y=c(100, 300, 200, 400)) df
> 
> ggplot(data = df, aes(x = x_order, y = y)) +   geom_point() +  
> geom_line()
> 
> ggplot(data = df, aes(x = x_labels, y = y)) +   geom_point() +  
> geom_line()

The first graph is what I want the order to be in, only with the x_labels as the actual labels. The second graph is the correct labels, but not the correct order.

Jordan
  • 1,415
  • 3
  • 18
  • 44
  • 1
    That's the wrong way to do this. Edit your `x_labels` column turning into a `factor` where the `levels` are in the right order. Then when you put that column on the x-axis, it will be ordered correctly. – Gregor Thomas Apr 02 '19 at 12:04
  • 1
    In your case, `df$x = factor(df$x_labels, levels = c("A_1", "A_2", "A_100", "A_200")); ggplot(data = df, aes(x = x, y = y)) + geom_point() + geom_line(aes(group = 1))` – Gregor Thomas Apr 02 '19 at 12:07
  • Thanks @Gregor. In the actual dataset, the labels are multiple variables in a tidy format. I have a loop running breaking them out by variable into their own graph. If I order that column, it would be 648 discrete variables. Is there a different way? – Jordan Apr 02 '19 at 12:10
  • 1
    You can order them programmatically. If you already have the `x_order` column defined, then `df$x = reorder(factor(df$x_labels), df$x_order)`. Alternatively, look at `?scale_x_discrete` - it has separate arguments for the `breaks` (the ticks, in some order) and `labels`. – Gregor Thomas Apr 02 '19 at 12:28
  • But, it's messy, not tidy, to have a label column and an order column. You'd be better off using whatever code you used to create the order column to instead order the levels of the label column and just have a single factor. – Gregor Thomas Apr 02 '19 at 12:37

0 Answers0