2015-04-04 124 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

相關問題