R 如何进行分组条形图?
我有以下称为标识的数据帧:R 如何进行分组条形图?,r,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
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,每一方有两列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
有人能告诉我我做错了什么吗
此外,我似乎无法获得名称识别。我的数据帧代码如下所示:
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)
ggplot需要data.frame类型的对象,而不是矩阵或表格。
您可以通过以下方式创建它:
identification <- data.frame(Party=c("CDU/CSU", "CDU/CSU", ...),
Type=c("Neg", "Pos", ...),
Resultat=c(meanNegCDUCSU, meanPosCDUCSU, ...))