且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何基于R ggplot2中的y轴值对x轴重新排序

更新时间:2023-11-26 12:21:46

我仅先删除了缺失的值.减号在我的机器上可以正常工作.

  df1< -df%>%过滤器(!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) 

由于缺少值,因此X_Variable 1、13和15排在最后.希望这会有所帮助.

  df%>%group_by(X_Variable)%>%总结(Y_Variable =平均值(Y_Variable))%&%;%排列(Y_Variable)#小动作:18 x 2X_Variable Y_Variable< int>< dbl>1 4 4.712 3 3.673 12 3.574 7 3.295 17 2.756 18 2.67 11 2.578 2 2.559 5 2.3310 6 2.311 9 212 16 213 8 1.8614 14 115 1不适用16 13不适用17 15不适用18不适用> 

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   
>