简体   繁体   English

Bar plot X轴不按数字顺序排列

[英]Bar plot X axis not in numerical order

I'm trying to plot using ggplot2 a bar graph.我正在尝试 plot 使用 ggplot2 条形图。 With the x values being different ranges ( SO x1= 0-10, x2= 11-20, x3= 21-30, ...... until 91-100, and the last range is ">100"). x 值是不同的范围(SO x1= 0-10,x2= 11-20,x3= 21-30,……直到 91-100,最后一个范围是“>100”)。 When i plot my graph with the corresponding y values as follows:当我 plot 我的图表与相应的 y 值如下:

ggplot(data=figure1_data, aes(x=Average.Coverage.of.Study, y=Number.of.Studies)) + geom_bar(stat = "Identity")

The ">100" x value comes first in the plot and not at the end which is where I want it. “>100”x 值首先出现在 plot 中,而不是我想要的末尾。 How would I get it to come after the 91-100 range x value??我如何让它在 91-100 范围 x 值之后出现? Can someone please help - I am very new to R. much appreciated:!有人可以帮忙吗 - 我是 R 的新手。非常感谢:! :) :)

Edited:编辑:

You need to order levels by your preference.您需要根据自己的喜好订购级别。

df <- data.frame(avg = c("0-10","11-20","21-30","91-100",">100"),
                 studies = c(1:5))
ggplot(df)+
  geom_bar(aes(x = ordered(avg, levels = c(avg)), y = studies), stat = "identity")

or或者

ggplot(df)+
  geom_bar(aes(x = ordered(avg, levels = c("0-10","11-20","21-30","91-100",">100")), y = studies), stat = "identity")` 

will both give you the same result.都会给你相同的结果。

有序条形图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM