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)