如何使用绑定数据更改x变量的顺序

时间:2017-03-18 15:37:09

标签: r ggplot2 rbind

我想改变下图中x变量的顺序,同时保持前后相同。换句话说,我想将数据集从最小的平均变化排列到最大的平均变化。我找到了一些代码但它真的意味着在单个数据框中排序x变量。但是,我将我的数据绑定在平均值之前和之后显示。这当然是按默认的字母顺序绘制的。任何建议将不胜感激。这是迄今为止的代码和数据框架:

数据框1

solm <-  data.frame(mean = 
                      c(1160,690,380,365,375,475, 725, 675,2685,675,415,905,
                        1135,3350,400,1460,385, 675, 415,2875), 
                    std.dev = c(760,310,0,15,50,125, 125, 300,765,300,35,555,
                                785,0,50,0,15, 325, 65,425),
                    class = c("previous"),
                    group = c("D.marsupialis","D.novemcinctus",
                              "P.maximus","T.tetrodactyla",
                              "C.albifrons","S.ignitus", "S.igniventris","C. paca",
                              "C. taczanowskii","D.punctata",
                              "A.microtis","P.flavus","E.barbara",
                              "M.frenata","L.pardalis","L.tigrinus",
                              "L.weidii","P.onca", "M.americana",
                              "M.chunyi"))
solm

##Data frame 2

obsm <- data.frame(mean = 
                     c(1290,825,460,930,920,1165,1050,1050,2740,1275,565,1226,
                       1900.5,2742.5,550,2363.5,445, 1050, 550,2750), 
                   std.dev = c(890,445,80,580,570,815, 450, 700,820,925,185,876,
                               1550.5,814.5,200,1097.5,75, 700, 200,550),
                   class = c("observed"),
                   group = c("D.marsupialis","D.novemcinctus",
                             "P.maximus","T.tetrodactyla",
                             "C.albifrons","S.ignitus", "S.igniventris", "C. paca",
                             "C. taczanowskii","D.punctata",
                             "A.microtis","P.flavus","E.barbara",
                             "M.frenata","L.pardalis","L.tigrinus",
                             "L.weidii","P.onca", "M.americana",
                             "M.chunyi"))

组合

d <- rbind(solm, obsm)
d

剧情

ggplot(d,aes(x=group, y=mean, col=class)) + 
  geom_pointrange(aes(ymin = mean+std.dev, ymax = mean-std.dev), 
                  position = position_dodge(0.5))+theme_bw()+ 
  labs(x="Mammal Species", y="Elevation (m)")+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank())+
  theme (axis.text.x = element_text(angle = 45, hjust = 1, 
                                    face="bold.italic"))+
  scale_y_continuous(breaks=c(300, 500, 1000, 1500, 2000, 2500, 
                              3000, 3500, 4000))

0 个答案:

没有答案