R:绘图:重新排列变量的顺序 [英] R: Plot: Re-arranging the order of variables

查看:68
本文介绍了R:绘图:重新排列变量的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 R 中创建一个条形图.但是,我想重新排列 x 轴上的变量,不是按频率而是按含义".

I want to create a barplot in R. However, I would like to re-arrange the variables on the x-axis, not by frequency but by 'meaning'.

假设我有以下数据集:

df<-data.frame(read.table(header=TRUE, text="
ID  Radio
1   A
2   B
3   A
4   C
5   D
6   D
7   E
8   E
9   A
10  B
11  C
12  E
13  C
14  A
15  D
16  A
17  C
18  A
19  A
20  F
21  A
22  C
23  C
24  A
25  B
26  A
27  C
28  A
29  B
30  C"))

我想用 plot 来描述频率.

I want to use plot to depict the frequencies.

plot(df$Radio)

显然,R 将创建一个条形图,按因子 df$radio(即 A B C D E F).但是,让我们假设顺序应该是:C E B A D <代码>F.(在这个场景背后的实际案例中,变量 dr$radio 代表受访者最后一次使用收音机的时间.C 代表今天",E 表示上周"等)

Obviously, R will create a barplot, ordered by the levels of the factor df$radio (i.e. A B C D E F). Let us, however, assume, the order should be: C E B A D F. (In the real-case case behind this scenario the variable dr$radio stands for the the last time the respondent has been using a radio. C stands for 'today', E for 'last week' etc.)

我不确定如何重新排列条形图中的顺序.我试图重新安排 df$radio 的级别顺序.然而,它只是弄乱了因子变量.此外,我试图通过在我的情节代码中使用订单"来解决这个问题,但也无济于事.有任何想法吗?非常感谢!

I am not sure what to do to re-arrange the order in my barplot. I tried to re-arrange the order of levels of df$radio. However, it just messed up the factor variable. Also, I tried to solve the problem by using `order' in my plot-code but to no avail, too. Any ideas? Much appreciated!

推荐答案

我们可以使用 factorlevels 指定

We can use factor with levels specified

plot(factor(df$Radio, levels=c("C", "E", "B", "A", "D", "F")))

这篇关于R:绘图:重新排列变量的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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