如何进行分组条形图?

时间:2015-03-04 17:03:13

标签: r

我有以下数据框称为“识别”:

Party      Type  Resultat
A CDU/CSU  Neg   0.468717413972888
B CDU/CSU  Pos   0.531282586027112
C SPD      Neg   0.438398776134625
D SPD      Pos   0.561601223865375
E FDP      Neg   0.60036231884058
F FDP      Pos   0.39963768115942
G LINKE    Neg   0.604554865424431
H LINKE    Pos   0.395445134575569
I GRUENE   Neg   0.515578406169666
J GRUENE   Pos   0.484421593830334
K PIRATEN  Neg   0.711750599520384
L PIRATEN  Pos   0.288249400479616
M AFD      Neg   0.694594594594595
N AFD      Pos   0.305405405405405

我正在尝试制作一个分组的条形图,Resultat在高处,并为每个Party制作两列(“Neg”和“Pos”)。

我在尝试时收到以下错误消息:

> ggplot(identification, aes(factor(Party), Resultat, fill=Type)) +
    + geom_bar(stat="identity", position = "dodge") +
    + scale_fill_brewer(palette = "Set1")

Erreur : ggplot2 doesn't know how to deal with data of class table

有人可以告诉我我做错了什么吗?

另外,我似乎无法获得names(identification)。我的数据框代码如下:

identification <- matrix(c("CDU/CSU", "Neg", meanNegCDUCSU,
                           "CDU/CSU", "Pos", meanPosCDUCSU,
                           "SPD", "Neg", meanNegSPD,
                           "SPD", "Pos", meanPosSPD,
                           "FDP", "Neg", meanNegFDP,
                           "FDP", "Pos", meanPosFDP,
                           "LINKE", "Neg", meanNegLINKE,
                           "LINKE", "Pos", meanPosLINKE,
                           "GRUENE", "Neg", meanNegGRUENE,
                           "GRUENE", "Pos", meanPosGRUENE,
                           "PIRATEN", "Neg", meanNegPIRATEN,
                           "PIRATEN", "Pos", meanPosPIRATEN,
                           "AFD", "Neg", meanNegAFD,
                           "AFD", "Pos", meanPosAFD), ncol=3, byrow=TRUE)

colnames(identification) <- c("Party", "Type", "Resultat")

identification <- as.table(identification)

1 个答案:

答案 0 :(得分:0)

ggplot需要data.frame类型的对象,而不是矩阵或表格。 你可以这样创建它:

 identification <- data.frame(Party=c("CDU/CSU", "CDU/CSU", ...),
                              Type=c("Neg", "Pos", ...),
                              Resultat=c(meanNegCDUCSU, meanPosCDUCSU, ...))