Home > Mobile >  How to arrange points in a particular ggplot?
How to arrange points in a particular ggplot?

Time:01-21

I am trying to plot the estimates of discrimination parameters from a Bayesian factor analysis model. Here is the code I have written:

plotting <- read.csv("mcmc_test_summary.csv")

plot  <-
  ggplot(data = plotting,
         aes(x = variable, y = Mean))  
  geom_point()   
  geom_errorbar(aes(ymin = (Mean - 1.96 * SD),
                    ymax = (Mean   1.96 * SD)), width = 0)  
  xlab("Variable")  
  ylab("Discrimination Parameter")  
  coord_flip()  
  geom_hline(yintercept = 0, linetype = 2)  
  theme_bw()
plot

This yields the following plot: enter image description here

I want the points to be arranged such that the lowest values start from the bottom and the highest is on top, thereby giving the plot a funnel-like shape. What is the best way to accomplish this?

Here are some sample data:

structure(list(Mean = c(0.220902135, 0.92194934, 0.951757326, 
0.840550594, -0.150822916, -0.329221189, 0.328728933, 0.339120204, 
0.175737064, -0.023259214, 0.518680416, 0.424380437, 0.069486045, 
0.387331157, -0.046630424, -0.479194489, -0.031897813, -0.032256682, 
-0.273993931, 0.001829565, 0.636323045, 0.075309094, 0.341434738
), SD = c(0.054641356, 0.044146774, 0.042821487, 0.046534239, 
0.057593389, 0.05600894, 0.054866676, 0.054378415, 0.056661627, 
0.056885927, 0.053209264, 0.053752882, 0.055792144, 0.054248883, 
0.055669106, 0.053439085, 0.056338517, 0.056278977, 0.055108844, 
0.05538902, 0.049911011, 0.045537741, 0.054748323), Naive.SE = c(0.000546414, 
0.000441468, 0.000428215, 0.000465342, 0.000575934, 0.000560089, 
0.000548667, 0.000543784, 0.000566616, 0.000568859, 0.000532093, 
0.000537529, 0.000557921, 0.000542489, 0.000556691, 0.000534391, 
0.000563385, 0.00056279, 0.000551088, 0.00055389, 0.00049911, 
0.000455377, 0.000547483), Time.series.SE = c(0.000537287, 0.000441468, 
0.000428215, 0.000465342, 0.000610109, 0.000560089, 0.000548667, 
0.000529486, 0.000557413, 0.000568859, 0.000538941, 0.000537529, 
0.000546465, 0.000528167, 0.000556691, 0.000534391, 0.000563385, 
0.00056279, 0.000551088, 0.000544834, 0.00049911, 0.000455377, 
0.000547483), variable = c("v2exremhsp_ord", "v2exdfdshs_ord", 
"v2exdfvths_ord", "v2exdfpphs_ord", "v2lgqstexp_ord", "v2lginvstp_ord", 
"v2lgoppart_ord", "v2lgfunds_ord", "v2lgcrrpt_ord", "v2lglegpup_ord", 
"v2lglegplo_ord", "v2exaphogp", "v2lgcomslo_ord", "v2lgsrvlo_ord", 
"v2lgstafflo_ord", "v2lgintbup", "v2exaphos", "v2ex_legconhos", 
"v2lgintblo", "v2lgtreaty", "v2lgwarlaw", "v2lgelecup", "v2lgello"
), parameter = c("discrimination", "discrimination", "discrimination", 
"discrimination", "discrimination", "discrimination", "discrimination", 
"discrimination", "discrimination", "discrimination", "discrimination", 
"discrimination", "discrimination", "discrimination", "discrimination", 
"discrimination", "discrimination", "discrimination", "discrimination", 
"discrimination", "discrimination", "discrimination", "discrimination"
), cilow = c(0.219831164, 0.921084063, 0.950918024, 0.839638523, 
-0.151951746, -0.330318965, 0.327653546, 0.338054387, 0.174626496, 
-0.024374179, 0.517637515, 0.423326881, 0.068392519, 0.386267879, 
-0.047721538, -0.480241895, -0.033002048, -0.03335975, -0.275074064, 
0.000743941, 0.635344789, 0.074416554, 0.340361671), cihigh = c(0.221973105, 
0.922814617, 0.952596627, 0.841462665, -0.149694086, -0.328123414, 
0.32980432, 0.340186021, 0.176847632, -0.02214425, 0.519723318, 
0.425433994, 0.070579571, 0.388394435, -0.045539309, -0.478147083, 
-0.030793578, -0.031153614, -0.272913798, 0.00291519, 0.637301301, 
0.076201633, 0.342507805)), class = "data.frame", row.names = c(NA, 
-23L))
        Mean         SD
1  0.2209021 0.05464136
2  0.9219493 0.04414677
3  0.9517573 0.04282149
4  0.8405506 0.04653424
5 -0.1508229 0.05759339
6 -0.3292212 0.05600894
     Naive.SE Time.series.SE
1 0.000546414    0.000537287
2 0.000441468    0.000441468
3 0.000428215    0.000428215
4 0.000465342    0.000465342
5 0.000575934    0.000610109
6 0.000560089    0.000560089
        variable      parameter
1 v2exremhsp_ord discrimination
2 v2exdfdshs_ord discrimination
3 v2exdfvths_ord discrimination
4 v2exdfpphs_ord discrimination
5 v2lgqstexp_ord discrimination
6 v2lginvstp_ord discrimination
       cilow     cihigh
1  0.2198312  0.2219731
2  0.9210841  0.9228146
3  0.9509180  0.9525966
4  0.8396385  0.8414627
5 -0.1519517 -0.1496941
6 -0.3303190 -0.3281234

CodePudding user response:

You could reorder your variable based on your Mean value like this:

library(ggplot2)
plot  <-
  ggplot(data = plotting,
         aes(x = reorder(variable, Mean), y = Mean))  
  geom_point()   
  geom_errorbar(aes(ymin = (Mean - 1.96 * SD),
                    ymax = (Mean   1.96 * SD)), width = 0)  
  xlab("Variable")  
  ylab("Discrimination Parameter")  
  coord_flip()  
  geom_hline(yintercept = 0, linetype = 2)  
  theme_bw()
plot

Created on 2023-01-20 with reprex v2.0.2


Using -Mean in reorder will do the opposite:

library(ggplot2)
plot  <-
  ggplot(data = plotting,
         aes(x = reorder(variable, -Mean), y = Mean))  
  geom_point()   
  geom_errorbar(aes(ymin = (Mean - 1.96 * SD),
                    ymax = (Mean   1.96 * SD)), width = 0)  
  xlab("Variable")  
  ylab("Discrimination Parameter")  
  coord_flip()  
  geom_hline(yintercept = 0, linetype = 2)  
  theme_bw()
plot

Created on 2023-01-20 with reprex v2.0.2

  • Related