在barplot中排序条() [英] Ordering bars in barplot()

查看:335
本文介绍了在barplot中排序条()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中一个变量(我们称之为Q1)有几个级别:无用,30分钟,1小时,2小时,3+小时 / p>

如何绘制一个barplot(),这些bar的顺序是因子级别?我尝试使用sort(),但这并没有办法。



编辑:根据要求,一些示例数据:

  Q1 
1小时
1小时
30分钟
2小时
3+小时
3小时以上
3+小时
3+小时
2小时
1小时
2小时
1小时
30分钟

我试过了:

  barplot(table(sort(Q1)),main =Q1 Answer Distribution,ylim = c(0,250),cex.axis = 0.9)

但它不会给我我需要的东西。

解决方案

正如Henrik所述,你需要将你的数据转化为一个因素(至少这是实现这个目标最简单的方法)。考虑下面一些假数据的例子...

$ $ p $ $ $ $ $ $#$> 1000个随机统一整数在1和5之间
data < - floor(runif(1000,1.6))

#使用给定标签产生数据因子
fdata < - 因子(数据,
标签= c 没有用,
30分钟,
1小时,
2小时,
3+小时))
$ b $ p
$ b

这可以通过绘图的base r来完成(当没有指定y时,barplot不是必需的)

  #in base R,只是使用plot  - 当y缺失时,产生barplot 
plot(fdata)

您也可以在ggplot2中绘图

  #in ggplot2 
require(ggplot2)

#创建一个数据帧
df< - data.frame(id = seq(1:length(fdata)),
fdata = fdata)

#plot via geom_bar
ggplot(df,aes(fd ata))+ geom_bar()

从原始示例开始,除了指定级别之外,您需要设置 ordered = TRUE ,如下所示。否则,无用仍会显示在列表的末尾。

pre $ #将数据转换为一个因子(提供的数据加上没有用)
q1 < - c(没有用
,1小时
,1小时
,30分钟
,2小时
,3+小时
,3+小时
,3+小时
,3+小时
,2小时
,1小时
,2小时
,1小时
,30分钟)

q1f =因子(q1,
levels = c ,
30分钟,
1小时,
2小时,
3+小时),
命令= TRUE)

然后,您可以应用上面显示的绘图逻辑...

I have a dataframe, and one of the variables (let's call it Q1) has several levels: "No use", "30 min", "1 hour", "2 hours", "3+ hours".

How can I plot a barplot(), where the bars are in the order of factor levels? I tried using sort(), but that doesn't do the trick.

EDIT: As requested, some sample data:

Q1
1 hour
1 hour
30 min
2 hours
3+ hours
3+ hours
3+ hours
3+ hours
2 hours
1 hour
2 hours
1 hour
30 min

I've tried:

barplot(table(sort(Q1)), main = "Q1 Answer Distribution", ylim = c(0, 250), cex.axis=0.9)

but it doesn't give me what I need.

解决方案

As stated by Henrik, you need to get your data into a factor (at least this is the easiest easiest way to make this happen). Consider the following example with some fake data...

#generate 1000 random uniform integers between 1 and 5
data <- floor(runif(1000, 1,6)) 

#make data a factor with given labels
fdata <- factor(data,
                labels = c("No use", 
                           "30 min", 
                           "1 hour", 
                           "2 hours", 
                           "3+ hours"))

This can be done in base r with plot (barplot is not required when y is not specified)

#in base R, just use plot - when y is missing, barplot is produced
plot(fdata)

You can also plot in ggplot2

#in ggplot2
require(ggplot2)

#make a dataframe
df <- data.frame(id = seq(1:length(fdata)),
                 fdata = fdata)

#plot via geom_bar
ggplot(df, aes(fdata)) + geom_bar()

Proceeding from your original example, in addition to specifying levels, you are going to need to set ordered=TRUE as shown below. Otherwise, "No use" will still show up at the end of your list.

#get data into a factor (provided data plus "No use")
q1 <- c("No use"
        ,"1 hour"
        ,"1 hour"
        ,"30 min"
        ,"2 hours"
        ,"3+ hours"
        ,"3+ hours"
        ,"3+ hours"
        ,"3+ hours"
        ,"2 hours"
        ,"1 hour"
        ,"2 hours"
        ,"1 hour"
        ,"30 min")

q1f = factor(q1,
             levels = c("No use", 
                          "30 min", 
                          "1 hour", 
                          "2 hours", 
                          "3+ hours"),
             ordered=TRUE)

Then you can apply the plot logic shown above...

这篇关于在barplot中排序条()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆