2017-07-22 17 views
0

私は子供の性的虐待研究に取り組んでいます。このために、latticeを使用してクラスター化された棒グラフを作成したいと考えています。私は次のコードを使用しました。lattice:Clustered Barchart:棒グラフをソートして棒の上に値を表示

マイデータ:

nature <- read.table(text = "Nature_of_sexual_abuse, Familiarity_with_the_perpetrator, Count 
        Talked in a sexual way, Not familiar at all, 21 
        Talked in a sexual way, Not very familiar, 22 
        Talked in a sexual way, Very familiar, 22 
        Shown pornography, Not familiar at all, 6 
        Shown pornography, Not very familiar, 17 
        Shown pornography, Very familiar, 16 
        Looked at private parts, Not familiar at all, 16 
        Looked at private parts, Not very familiar, 14 
        Looked at private parts, Very familiar, 10 
        Touched private parts, Not familiar at all, 6 
        Touched private parts, Not very familiar, 15 
        Touched private parts, Very familiar, 11 
        Made a sex video, Not familiar at all, 1 
        Made a sex video, Not very familiar, 6 
        Made a sex video, Very familiar, 7 
        Forced sex behaviors, Not familiar at all, 5 
        Forced sex behaviors, Not very familiar, 17 
        Forced sex behaviors, Very familiar, 10", 
        header = TRUE, 
        sep = ",", strip.white = TRUE) 

私のプロット:

library(lattice) 

colors = c("lightsalmon3", "lightgoldenrod2", "cadetblue4") 

barchart(
    data = nature, 
    origin = 0, 
    Count ~ Nature_of_sexual_abuse, 
    groups = Familiarity_with_the_perpetrator, 
    xlab = list (
    label = "Nature of sexual abuse", 
    font = 2, 
    cex = 1), 
    ylab= list (
    label = "Number of students", 
    font = 2, 
    cex = 1), 
    ylim=c(0,25), 
    labels = TRUE, 
    auto.key = list(space="top", columns= 3), 
    par.settings = list(superpose.polygon = list(col = colors)))  

チャートは現在、次のようになります。

enter image description here

しかし、私はそれは次のようにソートしたいですNature_of_sexual_abuseの変数:「メイドのセックスビデオ」、「強制的なセックス」 「秘密の部分を見る」、「触れられた秘密の部分」、「ポルノに描かれた」、「性的な方法で話された」などがあります。

各クラスタ内で、グラフはFamiliarity_with_the_perpetratorの変数でソートされていなければなりません。「非常によく知られています」、「あまりよく知られていません。

私はそれを表示したい順番でデータを送ろうとしました。しかし、latticeは自動的にグループをアルファベット順にソートしているようです。

私はまた、各バーの値がバーの一番上に表示されるようにします。誰かが私が望む順序でそれをグループ化するのを助けてくれますか?あなたが見ることができるように

、私はR.そうに初心者です、任意のヘルプは、多くのことを理解されるであろう。

+1

、あなたは[コメントで]リンクで与えられた解決策を試してみました(https://stackoverflow.com/questions/45222720/r-lattice-how-to-remove-white-space-between-バーとx軸#comment77414934_45222720)を入力してください。あなたがして、それが仕事をしなかった場合は、あなたの質問にこれを表示してください、そしてなぜそれが動作しなかったかを示してもらえますか? – user20650

答えて

1

あなたの列Nature_of_sexual_abuseFamiliarity_with_the_perpetratorはカテゴリ変数です要因です。タイプファクタのデータは、常にデータに存在する一意の文字値であるRのレベルを持ちます。これらのレベル値は、データをプロットするときにラベルとして使用されます。

たとえば、あなたのデータの列のレベルNature_of_sexual_abuseは、「セックスビデオ製」一意の値を持つ「強制セックス行為」、「プライベートの部分を見て」、「プライベートの部分に触れた」、「ポルノを示すの"と"性的な方法で話した "。

あなたはプロットでデータを並べ替えたい場合は、あなたのカテゴリデータのレベル値を並べ替えする必要があります。 プロットを作成する前に、この並べ替えを挿入し、それが動作するはずです:

library(lattice) 


nature <- read.table(text = "Nature_of_sexual_abuse, 
Familiarity_with_the_perpetrator, Count 
       Talked in a sexual way, Not familiar at all, 21 
       Talked in a sexual way, Not very familiar, 22 
       Talked in a sexual way, Very familiar, 22 
       Shown pornography, Not familiar at all, 6 
       Shown pornography, Not very familiar, 17 
       Shown pornography, Very familiar, 16 
       Looked at private parts, Not familiar at all, 16 
       Looked at private parts, Not very familiar, 14 
       Looked at private parts, Very familiar, 10 
       Touched private parts, Not familiar at all, 6 
       Touched private parts, Not very familiar, 15 
       Touched private parts, Very familiar, 11 
       Made a sex video, Not familiar at all, 1 
       Made a sex video, Not very familiar, 6 
       Made a sex video, Very familiar, 7 
       Forced sex behaviors, Not familiar at all, 5 
       Forced sex behaviors, Not very familiar, 17 
       Forced sex behaviors, Very familiar, 10", 
       header = TRUE, 
       sep = ",", strip.white = TRUE) 




nature$Nature_of_sexual_abuse <- factor(nature$Nature_of_sexual_abuse, 
            levels=c("Made a sex video", 
              "Forced sex behaviors", 
              "Looked at private parts", 
              "Touched private parts", 
              "Shown pornography", 
              "Talked in a sexual way")) 
nature$Familiarity_with_the_perpetrator <- 
factor(nature$Familiarity_with_the_perpetrator, levels=c("Very familiar", 
"Not very familiar","Not familiar at all")) 



colors = c("lightsalmon3", "lightgoldenrod2", "cadetblue4") 

barchart( data = nature, 
     origin = 0, 
     Count ~ Nature_of_sexual_abuse, 
     groups = Familiarity_with_the_perpetrator, 
     xlab = list (label = "Nature of sexual abuse", 
        font = 2, cex = 1), 
     ylab= list (label = "Number of students", 
        font = 2, cex = 1), 
     ylim=c(0,25), 
     labels = TRUE, 
     auto.key = list(space="top", columns= 3), 
     par.settings = list(superpose.polygon = list(col = colors)) 
     ) 

ごレベルの順序によっては、プロット内のデータの順序が変更されます。

私は、これはあなたのデータを発注の問題であなたを助けることを願っています。カウントのため

+0

ありがとうございます。 'barchart'関数を実行する前に、あなたが提供したコードを試しました。しかし、それは役に立たなかった。グラフは完全に空白に見えます。 –

+0

私はコードを実行すると完全に動作します。1)あなたはすでに – Elena

+0

@WhiteClubsが行ったように5)棒グラフを作成し、変数の色を作成します)3)私は4の上方に設けられたコードと因子レベルを並べ替え)が正しく2をあなたのパッケージをロードしてデータをデータフレーム_nature_を作成していることを確認してください。エレナの並べ替えを実行する前に、(自然な)$ Familiarity_with_the_perpetrator'を見ると、空白がある可能性があります。したがって、レベルベクトルは変数と一致しません。もしそうであれば、白いスペースを削除してください(なぜ、私はread.tableのstrip.white argumetnを追加しました) – user20650

関連する問題