2016-11-05 12 views
2

ggplot2(理想的には標準プロットの場合)を使用して、バープロット上に順序付け因子の値を表示したいとします。R - ggplot2で軸上に順序付けられた因子の値を表示する方法

私はこの1つのように命じた要因があります: "どのように練習に満足している:"

を - 絶対にない満足=>値1 - ない満足=>値2 - 満足= >値3 - 私は平均値と値「絶対に満足していない」とbarplotをプロットしたい非常に満足=>値4

- >「非常に満足」軸上の代わりに1 - > 4

ggplot2でそれを行うことはできますか?私の視点では、主な難しさは、値の分布ではなく、因子の平均をプロットすることです(実際には、私のプロットは順序付けられた因子を整数に変換して作成されます)。

これは私のデータセットのdputの結果です。

structure(c(3L, 2L, 3L, 2L, 2L, 3L, 2L, NA, 2L, 3L, 4L, 2L, 1L 
), .Label = c("pas du tout satisfait", "plutôt pas satisfait", 
"plutôt satisfait", "très satisfait"), class = c("ordered", 
"factor")) 

そしてここbarplotの一例である(軸上の値なし...):

example

コードは以下の通りです。

Toto <- structure(c(3L, 2L, 3L, 2L, 2L, 3L, 2L, NA, 2L, 3L, 4L, 2L, 1L 
), .Label = c("pas du tout satisfait", "plutôt pas satisfait", 
       "plutôt satisfait", "très satisfait"), class = c("ordered","factor")) 

TotoNumeric <- as.data.frame(as.integer(Toto)) 

DataForGggplot2 <- as.data.frame(round(sapply(X = TotoNumeric, FUN = "mean", na.rm = TRUE), 1)) 
colnames(DataForGggplot2) <- "Donnees" 
DataForGggplot2$Etiquette <- "the exercises" 

Graphe <- ggplot(data = DataForGggplot2, aes(x = Etiquette, y = Donnees)) + 
    geom_bar(stat = "identity", fill = "blue") + 
    scale_y_continuous(limits = c(0, 4)) 
    coord_flip() 

print(Graphe) 

ご要望が明確でない場合は、詳細をお知らせください。

おかげ

+0

私の例のx軸(質問のラベルではなく、値を持つ軸)。 – Kumpelka

+0

代わりに 'scale_y_continuous(limits = c(0、4)、labels = c(" "、levels(Toto)))を使ってみてください' – Nate

+1

ありがとうございました!最後にシンプル... – Kumpelka

答えて

1

あなたがこのために使用することができますscale_y_continuouslabelsと呼ばれるオプションがあります。

この例では、使用する係数がTotoであると仮定します。 0〜4(つまり長さ= 5)の制限を設定しているが、Totoのレベルは4つしかないので、0の値にはNAを追加しました。また、制限を0〜3または1〜4として設定することもできますNAレベルを追加します。詳細および例については

library(ggplot2) 

Toto <- structure(c(3L, 2L, 3L, 2L, 2L, 3L, 2L, NA, 2L, 3L, 4L, 2L, 1L 
), .Label = c("pas du tout satisfait", "plutôt pas satisfait", 
       "plutôt satisfait", "très satisfait"), class = c("ordered","factor")) 

TotoNumeric <- as.data.frame(as.integer(Toto)) 

DataForGggplot2 <- as.data.frame(round(sapply(X = TotoNumeric, FUN = "mean", na.rm = TRUE), 1)) 
colnames(DataForGggplot2) <- "Donnees" 
DataForGggplot2$Etiquette <- "the exercises" 

Graphe <- ggplot(data = DataForGggplot2, aes(x = Etiquette, y = Donnees)) + 
    geom_bar(stat = "identity", fill = "blue") + 
    scale_y_continuous(name="Toto", labels = c("NA", levels(Toto)),limits = c(0, 4)) 

coord_flip() 

print(Graphe) 

enter image description here

thisは偉大なリソースです。

+1

ありがとうございます。私は "NA"の値を必要としないので、私は 'limits = c(0,3)'を使ってみるが、 'limits = c(1,4)'は使わない。 – Kumpelka

+0

他の発言: 'limits ='は必須であるようです。私が削除しようとすると、エラーが発生しました_Error f(...、self = self):ブレークとラベルの長さが異なります。コードを単純化するために 'limits ='の使用を避けることは可能ですか? – Kumpelka

+0

よろしくお願いいたします。 'limits = c(1,4)'も動作しますが、それに応じて 'breaks'を設定するだけです。 'limits'が必要ですが、既存の最小値または最大値を参照するために' NA'に設定することができます。私はそれをお勧めしません。私は今試してみましたが、結果は悪かったです。プログラム的にしたい場合は、データの機能に制限を設定することができます(例: 'limits = c(1、max(df $ y)'。あなたの質問に答えられたら、緑色のチェックマークをクリックして将来の読者に答えさせてください。 –

関連する問題