2

I am trying to make the order from small to big when I plot a figure .

This is the data

df <- structure(list(label = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
4L, 5L, 5L, 6L, 6L), .Label = c(" data1", " data10", " data15", 
" data20", " data5", " data8"), class = "factor"), variable = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("boys", 
"girls"), class = "factor"), N = structure(c(1L, 1L, 2L, 2L, 
3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L), .Label = c("1", "10", "15", 
"20", "5", "8"), class = "factor"), value = c(93987048.3466667, 
54815262.22, 2050259639.66667, 2154175512.5, 3101847435.66667, 
2881192196, 6625604632, 3739286226.5, 270853386.15, 430516498, 
675222758.633333, 717219589.4), sd = c(129474855.769856, 48756976.8630242, 
777498079.458271, 301433971.74375, 1109031077.05936, 45164769.8052193, 
1060915464.90521, 1147931493.65351, 239740705.411582, 38453433.9415797, 
123930253.814345, 112112765.581137), se = c(74752342.8320143, 
34476388.97, 448888725.46965, 213146005.5, 640299390.87988, 31936315, 
612519829.250459, 811710143.5, 138414360.805088, 27190683.9, 
71551165.4004505, 79275696.8), ci = c(321633371.941334, 438064056.816815, 
1931412299.99575, 2708276784.58082, 2754985922.02623, 405789356.908967, 
2635460115.35016, 10313755269.7407, 595548927.354538, 345490396.550009, 
307859817.127003, 1007293234.14371)), .Names = c("label", "variable", 
"N", "value", "sd", "se", "ci"), row.names = c(NA, -12L), class = "data.frame")

based on this answer, I should be able to convert the label to character and then factor and then sort it. How do you specifically order ggplot2 x axis instead of alphabetical order?

So I did like this

df$N <- as.character(df$N)
#Then turn it back into an ordered factor
df$N <- factor(df$N, levels=unique(df$N))

If I str the df, I can see that it change the character to factor but the level does not change

I even tried to manually change the level

df$N <- factor(df$N, levels=c("1","5","8","10","15","20"))

but I am still unable to change the order in plot

so if i do df[order(df$N),] nothing happens

library(ggplot2)
pd <- position_dodge(0.1) 
ggplot(df, aes(x=N, y=value, colour=variable)) + 
    geom_errorbar(aes(ymin=value-se, ymax=value+se), width=.1, position=pd) +
    geom_line(position=pd) +
    geom_point(position=pd)
pogibas
  • 27,303
  • 19
  • 84
  • 117
  • 1
    `df$N <- factor(df$N, levels=c("1","5","8","10","15","20"))` worked for me. The x-axis of the plot is labeled (in order): 1, 5, 8, 10, 15, 20. – Nathan Werth Dec 01 '17 at 16:23
  • Setting the levels manually worked for me as well. – joran Dec 01 '17 at 16:24
  • @Nathan Werth what could be the problem that it does not work for me? which version of R are you using or package please? –  Dec 01 '17 at 16:26
  • I ran the statement immediately after creating the data from your example. If you ran a couple things between the two while experimenting, that could've affected the result. – Nathan Werth Dec 01 '17 at 16:29
  • @Nathan Werth it is very strange !!! you are right . Thanks for the point –  Dec 01 '17 at 16:31

2 Answers2

1

Convert N to character then to numeric and sort it.

# Sort N (Using OPs data)   
df$N2 <- factor(df$N, levels = sort(unique(as.numeric(as.character(df$N)))))

library(ggplot2)
ggplot(df, aes(N2, value, colour = variable)) + 
    geom_errorbar(aes(ymin = value - se, ymax = value + se), 
                  width = 0.1, position = pd) +
    geom_line(position = pd) +
    geom_point(position = pd)

enter image description here

pogibas
  • 27,303
  • 19
  • 84
  • 117
  • thanks, would you please tell why you convert it like that?? and why as a new label and not copy it on its own? –  Dec 01 '17 at 16:27
0

So it seems that there's a distinction between the problem you're proposing, and the question you linked to. In the question linked, they were using X, Y, and Z as representations of groups. They wanted those groups ordered in a specific way.

Here, you are using numbers, and you want them in ascending order. You shouldn't categorize them as factor. Rather, they should be categorized as numeric.

You may still load your numeric data in as a factor, but you'll need to convert it to character first, and then numeric.

df$N <- as.numeric(as.character(df$N))

pd <- position_dodge(0.1) 
ggplot(df, aes(x=N, y=value, colour=variable)) + 
  geom_errorbar(aes(ymin=value-se, ymax=value+se), width=.1, position=pd) +
  geom_line(position=pd) +
  geom_point(position=pd)

enter image description here