2015-04-04 83 views
0

我有一个看起来像这样如何使一个分组条形图两组在x轴上

Name, Clusters, incorrectly_classified 
PCA, 2, 34.37 
PCA, 6, 60.80 
ICA2, 2, 37.89 
ICA6, 2, 33.20 
ICA2, 6, 69.66 
ICA6, 6, 60.54 
RP2, 2, 32.94 
RP4, 2, 33.59 
RP6, 2, 31.25 
RP2, 6, 68.75 
RP4, 6, 61.58 
RP6, 6, 56.77 

我想创建一个barplot对于类似于这样的情节,我画了上述数据的数据

x轴将具有两个数字2或6.Y轴将具有incorrectly_classified并且Name将被绘制为每个26。每个组(2或6)的每个名称将在两组之间一致着色。

这是可能实现与条形图?如果不是条形图,那么绘制该数据的好方法是什么 enter image description here

+0

这是可能的ggplot和geom_bar。我可以发布解决方案,但我需要更多信息。 y轴将是“错误分类”值,但您希望将这些值合并在一起?例如倒数第三行和最后一行具有相同的名称和相同的簇,那么如何组合两个值(65.49和56.64)?和?意思? – jwilley44 2015-04-04 02:04:03

+0

@ jwilley44你可以忽略具有相同名称和相同群集的人。我编辑了问题并删除了这些行。 – birdy 2015-04-04 02:06:03

回答

3

我认为以下是你所追求的。

ggplot(data = mydf, aes(x = factor(Clusters), y = incorrectly_classified, fill = Name)) + 
geom_bar(stat = "identity", position = "dodge") + 
labs(x = "Clusters", y = "Incorrectly classified") 

enter image description here

+0

太棒了,看起来不错!另外,你的个人资料中的真棒飞行地图! – birdy 2015-04-04 02:21:15

+0

@birdy感谢您的评论。我很高兴这对你有所帮助。感谢您对飞行地图的评论! – jazzurro 2015-04-04 02:24:15

+0

我工作我的答案与barplot()随着R与其他答案,但我同意ggplot也是这个更好的选择 – jeborsel 2015-04-04 03:20:06

3

这可以通过barplot完成。

一个例子:

counts <- table(mtcars$vs, mtcars$gear) 
barplot(counts, main="Car Distribution by Gears and VS", 
    xlab="Number of Gears", col=c("darkblue","red"), 
    legend = rownames(counts), beside=TRUE) 

编辑

我还将努力我的答案,以证实该barplot选项(虽然ggplot是冷得多:-)):

如果DF是你的数据帧:

dfwide<-reshape(df,timevar="Clusters",v.names="incorrectly_classified",idvar="Name",direction="wide") 
rownames(dfwide) <- dfwide$Name 
dfwide$Name<-NULL 
names(dfwide)[names(dfwide)=="incorrectly_classified.2"] <- "2" 
names(dfwide)[names(dfwide)=="incorrectly_classified.6"] <- "6" 

dfwide<-as.matrix(dfwide) 

barplot(dfwide, main="Your Graph", 
     xlab="Clusters",ylab="incorrectly_classified",col=c("darkblue","red","orange","green","purple","grey"), 
     legend = rownames(dfwide), beside=TRUE,args.legend = list(x = "topleft", bty = "n", inset=c(0.15, -0.15))) 

enter image description here

相关问题