1

ヒストグラムの因子の順序付けに問題があります。私のデータは次のようなものです:

ID  onderlaag
1  strooisel
2  geen
3  strooisel
4  kniklaag
5  gras
6  geen
.
.

barplot() 関数を使用してヒストグラムを作成しました。

barplot(テーブル(onderlaag),ylim=c(0,250))

ここでのヒストグラム バーの順序はアルファベット順ですが、ストロイゼル - ジーン - グラ - クニクラーグの順序でさらに並べたいと思います。

因子関数を使用しましたが、これを行った後、バープロットにバーがなくなりました

onderlaag2=factor(onderlaag,levels=c("Strooisel","Geen","Gras","Kniklaag"))

これどうやってするの?

4

2 に答える 2

1

次回はサンプルデータを提供してください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 ) )
于 2013-01-31T12:24:18.687 に答える
1

あなたが求めているのは、入力を並べ替える方法にすぎないと思います。これは、次のように「barplot」関数の一部として簡単に実行できます。

barplot(table(onderlaag)[,c(4,1,2,3)], ylim=c(0,250))

「テーブル」機能は自動的に列を並べ替えますが、後で手動で順序を指定できます。その構文は次のように機能します。

table(your_data)[rows_to_select, columns_to_select]

your_dataはテーブルに作成されるデータ、は行rows_to_selectに適用されるフィルターcolumns_to_selectのリスト、 は列に適用されるフィルターのリストです。を指定しないrows_to_selectとすべての行が選択さcolumns_to_selectc(4,1,2,3)、 を指定すると 4 つの列すべてが特定の順序で選択されます。

于 2013-01-31T12:45:34.460 に答える