Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 直方图因子排序_R - Fatal编程技术网

R 直方图因子排序

R 直方图因子排序,r,R,我对直方图中的因子排序有问题。 我的数据是这样的: ID onderlaag 1 strooisel 2 geen 3 strooisel 4 kniklaag 5 gras 6 geen . . table(your_data)[rows_to_select, columns_to_select] 我已经使用barplot()函数制作了直方图: 条形图(表(onderlaag),ylim=c(0250)) 这里的柱状图条的顺序是按字母顺序排列的,但我想让它们按照:strooi

我对直方图中的因子排序有问题。 我的数据是这样的:

ID  onderlaag
1  strooisel
2  geen
3  strooisel
4  kniklaag
5  gras
6  geen
.
.
table(your_data)[rows_to_select, columns_to_select]
我已经使用barplot()函数制作了直方图:

条形图(表(onderlaag),ylim=c(0250))

这里的柱状图条的顺序是按字母顺序排列的,但我想让它们按照:strooisel-geen-gras-kniklag的顺序排列

我使用了因子函数,但我的条形图在我这样做后不再有条形图

onderlaag2=系数(onderlaag,等级=c(“Strooisel”、“Geen”、“Gras”、“Kniklag”))


我如何才能做到这一点?

请下次使用
dput提供示例数据

# construct an example data frame similar in structure to the question
x <- data.frame( ID = 1:4 , ord = c( 'b' , 'a' , 'b' , 'c' ) )

# look at the table of x, notice it's alphabetical
table( x )

# re-order the `ord` factor levels
levels( x$ord ) <- c( 'b' , 'a' , 'c' )

# look at x
x

# look at the table of x, notice `b` now comes first
table( x )

# print the results, even though it's not a histogram  ;)
barplot( table(x) , ylim = c( 0 , 5 ) )
#构造一个结构与问题类似的示例数据框

x我认为您要求的只是对输入进行排序的一种方式,我们可以很容易地将其作为“条形图”函数的一部分,如下所示:

barplot(table(onderlaag)[,c(4,1,2,3)], ylim=c(0,250))
“table”函数会自动为您的列排序,但您可以在之后手动指定顺序。它的语法如下所示:

ID  onderlaag
1  strooisel
2  geen
3  strooisel
4  kniklaag
5  gras
6  geen
.
.
table(your_data)[rows_to_select, columns_to_select]

其中,
your\u data
是将数据制成表的,
rows\u to\u select
是要应用于行的筛选器列表,
columns\u to\u select
是要应用于列的筛选器列表。通过不指定
rows\u to\u select
,我们选择了所有行,通过指定
columns\u to\u select
c(4,1,2,3)
,我们选择了所有四列,但按特定顺序进行选择。

如果您想要直方图,为什么不使用
hist()
?这实际上不是直方图,这将违反“历史”功能的预期。它只是一个有序的频率图,没有一组离散的、连续的x值。“barplot”和“table”函数只是构建此图的一种方法(我建议首先执行聚合函数),但我认为这超出了问题的范围。您不需要按“有序”创建。您需要做的就是按所需的顺序为factor函数指定levels向量。此外,data.frame调用首先创建了x$ord作为因子,因此您不是在转换为因子,而是在重新调整现有因子。可以在以下情况下完成:
级别(x$ord)已同意。。你只需要
x$ord谢谢你的回答,安东尼。这真的很有帮助