2017-07-13 65 views
-1

我的數據如下分組條形圖中的R與分類數據

Q1     Q2     Q3     Q4  
Strongly Agree  Strongly Agree  Agree    Agree 
Agree    Strongly Agree  Agree    Strongly Agree 
Strongly Agree  Disagree   Strongly Disagree Disagree 
Disagree    Agree    Strongly Agree  Agree 
Agree    Agree    Disagree    Disagree 
Strongly Disagree Strongly Disagree Disagree    Disagree 

我想使具有頻率高度的分組柱狀圖。圖表底部的組是「非常同意」,「同意」,「不同意」和「非常不同意」。然後在每個組中會有不同的問題。

這是我到目前爲止有:

Question <- c(df$`Q24#1_4`, df$`Q24#1_1`, df$`Q24#1_2`, df$`Q24#1_3`) 
ggplot(Question, aes(x = c('Strongly Disagree', 'Disagree', 'Agree', 
          'Sgrongly Agree'))) + geom_bar() 
+0

你應該[整齊](http://garrettgman.github.io/tidying/)數據先用'tidyr ::收集(DF)'。 –

回答

1

首先,你需要繪製(你可以使用melt爲。有許多不同的方式來做到這一點)

之前解決您的數據(1)。您可以創建一個id柱(即與rownames)融化你的數據

data$id<-rownames(data)

(2)。現在,您可以使用新列

library(reshape)融化你的數據

data_melt<-melt(data, id="id")

(3)。該數據已準備好繪製...

ggplot(data_melt,aes(factor(value)))+geom_bar(aes(fill = variable), position = "dodge")

enter image description here