I am trying to reorder (I don't mind whether it is ascending or descending order) the x-axis on my errorplot based on the mean values of the y-axis. I have applied a solution based on this post , however for some reason it seems to be ignoring the reorder
command. Any ideas what is happening here?
#Import data.
df <- structure(list(X_Variable = c(4L, 4L, 13L, 18L, 12L, 3L, 15L,
NA, 18L, 4L, 17L, NA, 3L, 15L, 4L, 6L, 12L, NA, 2L, 1L, NA, 15L,
1L, 6L, 1L, 12L, NA, 6L, NA, 15L, NA, 1L, 7L, 15L, 11L, NA, NA,
1L, 1L, 7L, 2L, 2L, 12L, 11L, 15L, 17L, 1L, 4L, 11L, 15L, 2L,
3L, 13L, 17L, 15L, 6L, 3L, 14L, 12L, 8L, 12L, 11L, NA, 2L, 11L,
NA, 4L, 8L, 15L, 4L, 7L, 8L, 15L, 15L, 15L, 6L, 3L, 6L, 8L, 15L,
4L, 2L, 1L, 1L, 7L, 17L, 15L, 1L, NA, 5L, 13L, 1L, 15L, 4L, 15L,
13L, 18L, 1L, 15L, 6L, NA, 6L, NA, 6L, 1L, 16L, 4L, 1L, NA, 2L,
12L, NA, 7L, 2L, 15L, 13L, 13L, 16L, NA, 7L, 2L, 4L, 15L, 11L,
15L, 2L, 5L, 13L, 2L, 9L, 7L, 6L, 15L, 15L, 11L, 3L, 15L, 13L,
NA, 4L, 8L, NA, 4L, 8L, 18L, 4L, 1L, 8L, 5L, 18L), Y_Variable = c(6L,
4L, 5L, 4L, 4L, 3L, 7L, 1L, 1L, 7L, 4L, NA, 5L, 1L, 6L, 1L, 6L,
3L, 6L, 4L, NA, 4L, 6L, 5L, 1L, 4L, 1L, 1L, 6L, 3L, 4L, 1L, 1L,
2L, 3L, 4L, 4L, 2L, 2L, 2L, 4L, 1L, 1L, 5L, 4L, 1L, 4L, 4L, 3L,
3L, 2L, 2L, 1L, 3L, NA, 2L, 4L, 1L, 2L, 2L, 6L, 3L, NA, 2L, 2L,
NA, 4L, 2L, 3L, 6L, 5L, 4L, 1L, 5L, 3L, 1L, 4L, 6L, 1L, 5L, 4L,
2L, 1L, 5L, 4L, 3L, 2L, NA, 4L, 2L, NA, 4L, 5L, 5L, 4L, 2L, 1L,
5L, 2L, 2L, 4L, 1L, 4L, 1L, 5L, 2L, 1L, 3L, NA, 2L, 2L, 2L, 5L,
1L, 1L, 4L, 2L, 2L, NA, 3L, 5L, 7L, 1L, 1L, 1L, 1L, 4L, 1L, 2L,
2L, 3L, 3L, 3L, 4L, 1L, 4L, 3L, 4L, 3L, 6L, 1L, 5L, 4L, 2L, 5L,
2L, 3L, 1L, 1L, 2L)), row.names = c(NA, -150L), class = "data.frame")
#Error plot ordered by Y-Variable.
ggplot(df, aes(x=reorder(X_Variable, Y_Variable, FUN=mean), y=Y_Variable))+
geom_point(stat="summary", fun.y="mean")+
geom_errorbar(stat="summary", fun.data="mean_se", fun.args=list(mult=1.96), width=0.1)
I only removed missing values first. The minus sign works fine on my machine.
df1<-df %>%
filter(!is.na(X_Variable), !is.na(Y_Variable))
ggplot(df1, aes(x=reorder(X_Variable, -Y_Variable, FUN=mean), y=Y_Variable))+
geom_point(stat="summary", fun.y="mean")+
geom_errorbar(stat="summary", fun.data="mean_se", fun.args=list(mult=1.96), width=0.1)
Edit: Because of missing values, X_Variable 1, 13, and 15 ranked last. Hope this helps.
df %>% group_by(X_Variable) %>%
summarise(
Y_Variable = mean(Y_Variable)) %>%
arrange(Y_Variable)
# A tibble: 18 x 2
X_Variable Y_Variable
<int> <dbl>
1 4 4.71
2 3 3.67
3 12 3.57
4 7 3.29
5 17 2.75
6 18 2.6
7 11 2.57
8 2 2.55
9 5 2.33
10 6 2.3
11 9 2
12 16 2
13 8 1.86
14 14 1
15 1 NA
16 13 NA
17 15 NA
18 NA NA
>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.