ggplot2:重新排列条形图中的条从最高到最低[重复](ggplot2: reorder bars in barplot from highest to lowest [duplicate])
这个问题在这里已经有了答案:
在geom_bar ggplot2中重新排序条 1答案我有这个数字
在类似问题的答案之后
library(ggplot2) library(egg) mydf <- transform(mydf, variables = reorder(variables, VIP, decreasing = T)) p1 <- ggplot(mydf, aes(x = variables, y = VIP, group =1))+ geom_bar(stat="identity") + geom_hline(yintercept = 1, size = 2, linetype = 3) + theme(axis.title.x =element_blank()) p2 <- ggplot(mydf, aes(x = variables, y = coefficient, group =1))+ geom_point()+ geom_line()+ geom_hline(yintercept = 0, size = 2, linetype = 3) grid.draw(egg::ggarrange(p1,p2 , ncol=1))我的目标是将酒吧从高到低排列。
尽管我将variables and VIP从最高排序到最低排列,但排列顺序却从最低到最高。
任何想法出了什么问题,并使酒吧从最低到最高排序?
数据
mydf <- read.table(text = c(" variables VIP coefficient diesel 0.705321 0.19968224 twodoors 1.2947119 0.3387236 sportsstyle 0.8406462 -0.25861398 wheelbase 1.3775179 -0.42541873 length 0.8660376 0.09322408 width 0.8202489 0.27762277 height 1.0140934 -0.12334574 curbweight 0.996365 -0.29504266 enginesize 0.8601269 -0.25321317 horsepower 0.7093094 0.16587358 horse_per_weight 1.2389938 0.43380122"), header = T)This question already has an answer here:
Reorder bars in geom_bar ggplot2 2 answersI got this figure
Following the answer of similar question
library(ggplot2) library(egg) mydf <- transform(mydf, variables = reorder(variables, VIP, decreasing = T)) p1 <- ggplot(mydf, aes(x = variables, y = VIP, group =1))+ geom_bar(stat="identity") + geom_hline(yintercept = 1, size = 2, linetype = 3) + theme(axis.title.x =element_blank()) p2 <- ggplot(mydf, aes(x = variables, y = coefficient, group =1))+ geom_point()+ geom_line()+ geom_hline(yintercept = 0, size = 2, linetype = 3) grid.draw(egg::ggarrange(p1,p2 , ncol=1))My goal was to order the bars from highest to lowest.
Although, I sorted the variables and VIP from highest to lowest, the bars were ordered from lowest to highest.
Any idea what went wrong and made the bars sorted from lowest to highest?
Data
mydf <- read.table(text = c(" variables VIP coefficient diesel 0.705321 0.19968224 twodoors 1.2947119 0.3387236 sportsstyle 0.8406462 -0.25861398 wheelbase 1.3775179 -0.42541873 length 0.8660376 0.09322408 width 0.8202489 0.27762277 height 1.0140934 -0.12334574 curbweight 0.996365 -0.29504266 enginesize 0.8601269 -0.25321317 horsepower 0.7093094 0.16587358 horse_per_weight 1.2389938 0.43380122"), header = T)最满意答案
问题是由于错误使用reorder 。
library(ggplot2) library(egg) mydf <- transform(mydf, variables = reorder(variables, -VIP)) p1 <- ggplot(mydf, aes(x = variables, y = VIP))+ geom_bar(stat="identity") + geom_hline(yintercept = 1, size = 2, linetype = 3) + theme(axis.title.x =element_blank()) p2 <- ggplot(mydf, aes(x = variables, y = coefficient, group = 1))+ geom_point()+ geom_line()+ geom_hline(yintercept = 0, size = 2, linetype = 3) grid.draw(egg::ggarrange(p1,p2 , ncol=1))
The problem is due to a wrong use of reorder.
library(ggplot2) library(egg) mydf <- transform(mydf, variables = reorder(variables, -VIP)) p1 <- ggplot(mydf, aes(x = variables, y = VIP))+ geom_bar(stat="identity") + geom_hline(yintercept = 1, size = 2, linetype = 3) + theme(axis.title.x =element_blank()) p2 <- ggplot(mydf, aes(x = variables, y = coefficient, group = 1))+ geom_point()+ geom_line()+ geom_hline(yintercept = 0, size = 2, linetype = 3) grid.draw(egg::ggarrange(p1,p2 , ncol=1))更多推荐
发布评论