如何在ggplot2中用彩色条表示不同的类别?

时间:2015-11-03 17:31:16

标签: r ggplot2

我对R比较陌生,我试图在ggplot2中建立一个条形图。我想为不同的颜色添加颜色以指示哪些" Group"他们属于,但当我绘制这个时,我发现传说的顺序与"饮食项目"的顺序不符。 (在x轴上)。图例以默认的字母顺序列出。

使图例的顺序与情节匹配的最佳方法是什么?

我的数据如下:

 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))

1 个答案:

答案 0 :(得分: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的级别设置为在给定data.frame中声明的顺序:

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