2015-11-03 32 views
1

我對R比較陌生,我試圖在ggplot2中創建一個barplot。我想爲不同的小節着色,以指示它們屬於哪個「組」,但是當我繪製該圖時,我發現圖例的順序與「飲食項目」的順序不同(在x軸上)。圖例以默認的字母順序列出。如何在ggplot2中顯示顏色條以指示不同的類別?

使圖例的順序與圖表匹配的最佳方式是什麼?

我的數據是這樣的:

str(Diet) 
'data.frame': 27 obs. of 4 variables: 
$ Group  : Factor w/ 7 levels "algae","crustacean",..: 1 1 1 1 1 5 5 5 5 5 ... 
$ Category : Factor w/ 27 levels "algae","biofilm",..: 1 2 8 11 21 4 9 10 14 15 ... 
$ studies_n : int 61 4 8 18 2 59 90 76 57 119 ... 
$ studies_pc: num 38.4 2.5 5 11.3 1.3 37.1 56.6 47.8 35.8 74.8 ... 

head(Diet) 
    Group  Category studies_n studies_pc 
1 algae   algae  61  38.4 
2 algae  biofilm   4  2.5 
3 algae  diatoms   8  5.0 
4 algae  fil alg  18  11.3 
5 algae phytoplankton   2  1.3 
6 insect Coleoptera  59  37.1 

,我已經建立的情節看起來像這樣:

plot of diet categories

這裏是我的代碼:

abPalette <- c("#009E73","#E69F00","#000000","#999999", "#56B4E9", "#F0E442", "#0072B2", "#D55E00", "#CC79A7") 

barplot3 <- qplot(xlab="Diet Items", ylab="Number of studies", x=Diet$Category, y=Diet$studies_n, fill=Diet$Group, geom="bar", stat="identity") + coord_flip() + labs(fill="Diet Group") +scale_x_discrete(limits = rev(Diet$Category)) + scale_fill_manual(values=abPalette) 

barplot3 

我有一直在研究這幾天,並嘗試了幾種方法,我仍然願意如果有更簡單的方法可以將它全部用於新代碼!

添加dput:

dput(Diet) 
structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 4L, 2L, 2L, 2L, 3L, 
3L, 7L, 7L, 7L), .Label = c("algae", "crustacean", "detritus", 
"fish", "insect", "mollusc", "other"), class = "factor"), Category = structure(c(1L, 
2L, 8L, 11L, 21L, 4L, 9L, 10L, 14L, 15L, 16L, 17L, 19L, 22L, 
25L, 3L, 13L, 18L, 12L, 5L, 6L, 27L, 7L, 24L, 20L, 23L, 26L), .Label = c("algae", 
"biofilm", "Bivalvia", "Coleoptera", "Crustacea", "Decapoda", 
"detritus", "diatoms", "Diptera", "Ephemeroptera", "fil alg", 
"fish", "Gastropoda", "Hemiptera", "insects", "Lepidoptera", 
"Megaloptera", "Mollusca", "Odonata", "other", "phytoplankton", 
"Plecoptera", "terr invert", "terr veg", "Trichoptera", "vertebrate", 
"zooplankton"), class = "factor"), studies_n = c(61L, 4L, 8L, 
18L, 2L, 59L, 90L, 76L, 57L, 119L, 41L, 13L, 53L, 49L, 80L, 7L, 
50L, 12L, 114L, 13L, 55L, 90L, 116L, 112L, 25L, 56L, 2L), studies_pc = c(38.4, 
2.5, 5, 11.3, 1.3, 37.1, 56.6, 47.8, 35.8, 74.8, 25.8, 8.2, 33.3, 
30.8, 50.3, 4.4, 31.4, 7.5, 71.7, 8.2, 34.6, 56.6, 73, 70.4, 
15.7, 35.2, 1.3)), .Names = c("Group", "Category", "studies_n", 
"studies_pc"), class = "data.frame", row.names = c(NA, -27L)) 
+0

你可以把你的數據的'dput'在你的問題? – Heroka

+0

請爲您的代碼使用正確的格式! –

+0

這是你需要的嗎? – ayesha

回答

0

也許最簡單的解決方法就是重新排序Diet data.frame前期:

Diet <- Diet[order(Diet$Group), ] 

ggplot(Diet, aes(x = Category, y = studies_n, fill = Group)) + 
    geom_bar(stat = "identity") + 
    coord_flip() + 
    scale_x_discrete(limits = rev(Diet$Category)) 

Plot01

或者,您可以設置的水平Diet$Group按照給定數據中的順序排列。框架:

Diet$Group <- factor(Diet$Group, levels = unique(Diet$Group)) 

ggplot(Diet, aes(x = Category, y = studies_n, fill = Group)) + 
    geom_bar(stat = "identity") + 
    coord_flip() + 
    scale_x_discrete(limits = rev(Diet$Category)) 

Plot02

+0

你是我的英雄!這種簡單的解決方案,當你知道如何。 :) – ayesha

相關問題