不同ggplot中的图例颜色相同

时间:2016-03-01 21:46:33

标签: r ggplot2 legend

我正在绘制数据如下,并通过循环以tiff格式导出图像:

library(ggplot2)
library(tidyr)
library(dplyr)
library(scales)

mydata<-read.csv("ex.csv",sep=";")
mydata$Categorie <- as.character(mydata$Categorie)

for(i in 1:12)
{    
DF=mydata[mydata$Portion_Longueur==i,]
cats <- unique(unlist(strsplit(DF$Categorie, ",")))  
cat_perc <- function(cats, vec) {
  # percentages
  nums <- sapply(cats, function(cat) sum(grepl(cat, vec)))
  perc <- nums/sum(nums)
  final <- perc * length(vec)
  df <- as.data.frame(as.list(final))
  names(df) <- cats
  return(df)
}

cat_perc(cats, DF$Categorie) #because I've combined category, i've assigned weight for each category in case of combination

plot_data <-
group_by(DF, Destination) %>%
  do(cat_perc(cats, .$Categorie))
plot_data

plot_data <-  gather(plot_data, Categorie, value, -Destination)
head(plot_data)

zone=table(DF$Destination)
zone=zone[which(zone>0)]
nbdest=dim(zone)

ggplot(plot_data, aes(reorder(Destination, -value), y = (value/dim(DF)[1])*100, fill = Categorie)) +  geom_bar(stat = "identity") + theme(
    panel.grid.major.y = element_line(colour = "black", linetype = 3, size = .5),
    panel.background = element_blank(),
    axis.title.x = element_text(size=8),
    axis.text.x = element_text(size=8, angle=45, hjust=1, vjust=1),
    axis.title.y = element_text(size=8, angle = 90),
    axis.text.y = element_text(size=8),
    strip.background = element_rect(color="white", fill="white"),
    strip.text = element_text(size=8)
  ) + scale_x_discrete(name ="Destination") + ggtitle(paste("Zone ",i," / Nb Q ",dim(DF)[1]," / Dest ",nbdest, sep=""))   + 
     theme(plot.title = element_text(size=16,lineheight=2, face="bold")) +         
     scale_y_continuous(name=" Pourcentage %",limits = c(0,25)) 
     ggsave(filename=paste("zone_destination_cat_pond_", i, ".tiff", sep = ""))

}

问题在于从一个情节到另一个情节,当存在不存在的类别时,图例会发生变化,而我想用视觉比较所有情节和相同的颜色代码。

数据:

> dput(mydata)
structure(list(Date = structure(c(11L, 1L, 9L,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, 6L, 3L, 5L, 6L, 
11L, 4L, 9L, 5L, 6L, 6L, 5L, 8L, 3L, 6L), .Label = c("01/02/2016", 
"02/02/2016", "03/02/2016", "08/02/2016", "10/02/2016", "11/02/2016", 
"16/02/2016", "22/02/2016", "26/01/2016", "27/01/2016", "28/01/2016"
), class = "factor"), Destination = structure(c(30L, 33L, 11L, 
1L, 18L, 18L, 11L, 16L, 19L, 19L, 22L, 1L, 18L, 18L, 13L, 14L, 
13L, 18L, 24L, 24L, 11L, 24L, 2L, 33L, 25L, 1L, 30L, 5L, 24L, 
18L, 13L, 35L, 19L, 19L, 18L, 23L, 19L, 8L, 19L, 14L, 28L, 22L, 
1L, 25L, 19L, 19L, 29L, 9L, 18L, 13L, 30L, 23L, 23L, 19L, 18L, 
18L, 11L, 33L, 13L, 24L, 19L, 24L, 18L, 25L, 23L, 18L, 13L, 13L, 
24L, 14L, 24L, 23L, 19L, 30L, 14L, 4L, 20L, 24L, 24L, 24L, 24L, 
24L, 12L, 22L, 5L, 28L, 32L, 13L, 14L, 28L, 14L, 24L, 28L, 25L, 
24L, 19L, 24L, 13L, 11L, 19L, 23L, 5L, 1L, 19L, 25L, 14L, 13L, 
23L, 25L, 34L, 34L, 13L, 14L, 34L, 1L, 1L, 35L, 18L, 14L, 11L, 
13L, 25L, 33L, 13L, 24L, 11L, 24L, 24L, 30L, 21L, 24L, 24L, 34L, 
18L, 1L, 19L, 13L, 30L, 19L, 13L, 23L, 25L, 22L, 19L, 30L, 13L, 
18L, 14L, 33L, 14L, 24L, 14L, 28L, 24L, 5L, 8L, 11L, 11L, 24L, 
7L, 30L, 28L, 14L, 35L, 10L, 24L, 14L, 28L, 24L, 19L, 9L, 25L, 
22L, 25L, 14L, 13L, 35L, 37L, 13L, 22L, 30L, 19L, 18L, 24L, 19L, 
14L, 14L, 26L, 19L, 18L, 13L, 13L, 13L, 14L, 13L, 25L, 14L, 25L, 
13L, 14L, 35L, 25L, 25L, 38L, 19L, 21L, 31L, 19L, 26L, 5L, 5L, 
13L, 23L, 19L, 14L, 14L, 19L, 16L, 19L, 33L, 26L, 16L, 25L, 14L, 
14L, 14L, 33L, 16L, 14L, 25L, 33L, 33L, 35L, 19L, 33L, 33L, 14L, 
2L, 35L, 35L, 33L, 26L, 33L, 33L, 26L, 19L, 33L, 35L, 14L, 19L, 
24L, 33L, 14L, 14L, 13L, 14L, 14L, 19L, 18L, 7L, 35L, 33L, 25L, 
19L, 33L, 14L, 19L, 33L, 33L, 2L, 33L, 33L, 14L, 2L, 19L, 33L, 
33L, 33L, 14L, 19L, 11L, 24L, 18L, 6L, 8L, 7L, 30L, 6L, 20L, 
33L, 30L, 6L, 19L, 14L, 25L, 33L, 8L, 24L, 24L, 6L, 19L, 19L, 
33L, 19L, 18L, 7L, 24L, 1L, 20L, 18L, 28L, 35L, 1L, 8L, 1L, 18L, 
19L, 25L, 24L, 26L, 19L, 28L, 11L, 28L, 11L, 24L, 33L, 1L, 18L, 
24L, 18L, 13L, 7L, 13L, 11L, 28L, 19L, 18L, 19L, 24L, 20L, 30L, 
18L, 8L, 28L, 11L, 20L, 25L, 24L, 26L, 11L, 15L, 13L, 22L, 1L, 
19L, 27L, 25L, 25L, 28L, 11L, 11L, 24L, 1L, 28L, 11L, 26L, 17L, 
23L, 29L, 25L, 19L, 18L, 18L, 13L, 13L, 25L, 25L, 13L, 11L, 28L, 
18L, 27L, 5L, 34L, 13L, 21L, 9L, 27L, 13L, 34L, 14L, 22L, 21L, 
35L, 37L, 21L, 18L, 18L, 18L, 31L, 14L, 14L, 35L, 35L, 22L, 14L, 
14L, 28L, 34L, 5L, 35L, 37L, 21L, 25L, 14L, 21L, 13L, 22L, 22L, 
14L, 14L, 14L, 14L, 5L, 14L, 38L, 5L, 22L, 5L, 22L, 5L, 14L, 
5L, 5L, 22L, 21L, 5L, 13L, 5L, 3L, 5L, 14L, 14L, 14L, 21L, 19L, 
24L, 18L, 23L, 21L, 5L, 22L, 38L, 13L, 5L, 35L, 22L, 13L, 5L, 
14L, 5L, 22L, 22L, 26L, 18L, 25L, 5L, 19L, 6L, 1L, 18L, 19L, 
25L, 38L, 8L, 14L, 37L, 14L, 2L, 13L, 26L, 34L, 24L, 24L, 11L, 
38L, 26L, 18L, 34L, 14L, 24L, 16L, 24L, 26L, 7L, 28L, 14L, 25L, 
28L, 25L, 23L, 36L, 13L, 25L, 18L, 33L, 25L, 34L, 19L, 13L, 11L, 
13L, 33L, 14L, 31L, 20L, 25L, 14L, 13L, 35L), .Label = c("ARD", 
"ARP", "BBB", "BIE", "CFX", "CHR", "DDD", "DOO", "EAU", "ELY", 
"EPI", "ETR", "GEN", "GER", "GGG", "GIS", "ISE", "JUV", "LER", 
"LES", "LON", "LYR", "MON", "NER", "NGY", "NOJ", "NYO", "ORI", 
"PEO", "RAY", "RRR", "RSI", "SEI", "SEP", "VIL", "XQU", "YYY", 
"ZYZ"), class = "factor"), Categorie = c("1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1,2", 
"1,2", "1,2", "1,2", "1,2", "1,2", "1,2", "1,2", "1,2", "1,2", 
"1,2", "1,2", "1,2", "1,2", "1,2", "1,3", "1,3", "1,3", "1,3", 
"1,3", "1,3", "1,3", "1,3", "1,3", "1,3", "1,3", "1,3", "1,3", 
"1,3", "1,3", "1,3", "1,3", "1,3", "1,3", "1,3", "1,3", "1,3", 
"1,3", "1,3", "1,3", "1,3", "1,3", "1,3", "1,3", "1,3", "1,3", 
"1,3", "1,3", "1,3", "1,3", "1,3", "1,3", "1,3", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2,3", "2,3", 
"2,3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3", "3", "4", "4", "4", "4", 
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", 
"5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", 
"5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", "5", 
"5", "5", "3", "1,2,3", "1,2,3", "1,2,3", "1,2,3", "1,2,3", "1,2,3"
), Portion_Longueur = c(3L, 4L, 1L, 1L, 2L, 4L, 5L, 6L, 7L, 7L, 
8L, 8L, 9L, 8L, 8L, 9L, 11L, 7L, 7L, 7L, 9L, 8L, 3L, 8L, 7L, 
11L, 2L, 9L, 8L, 5L, 8L, 12L, 3L, 4L, 1L, 3L, 3L, 3L, 4L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 10L, 4L, 4L, 5L, 6L, 6L, 7L, 
8L, 9L, 10L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 7L, 7L, 7L, 8L, 
6L, 6L, 6L, 9L, 4L, 6L, 1L, 1L, 1L, 1L, 1L, 1L, 12L, 12L, 12L, 
11L, 11L, 12L, 12L, 12L, 12L, 12L, 11L, 11L, 5L, 5L, 6L, 6L, 
6L, 10L, 8L, 8L, 8L, 8L, 9L, 5L, 6L, 3L, 7L, 7L, 7L, 1L, 7L, 
12L, 6L, 6L, 7L, 6L, 6L, 4L, 9L, 7L, 7L, 5L, 8L, 5L, 5L, 5L, 
6L, 10L, 6L, 7L, 9L, 7L, 9L, 10L, 11L, 11L, 9L, 11L, 9L, 10L, 
10L, 2L, 12L, 9L, 9L, 7L, 2L, 10L, 9L, 4L, 4L, 5L, 9L, 5L, 6L, 
8L, 2L, 2L, 3L, 7L, 8L, 10L, 11L, 3L, 1L, 5L, 5L, 7L, 7L, 7L, 
8L, 6L, 12L, 6L, 6L, 8L, 9L, 7L, 9L, 9L, 8L, 1L, 3L, 12L, 12L, 
9L, 8L, 9L, 10L, 9L, 10L, 10L, 10L, 10L, 12L, 11L, 9L, 11L,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, 8L, 10L, 9L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 5L, 7L, 3L, 10L, 8L, 4L, 4L, 4L, 9L, 12L, 
8L, 2L, 4L, 5L, 7L, 9L, 9L, 4L, 5L, 5L, 6L, 10L, 1L, 2L, 2L, 
3L, 3L, 4L, 6L, 6L, 6L, 6L, 8L, 10L, 5L, 6L, 6L, 7L, 8L, 9L, 
8L, 9L, 8L, 9L, 6L, 8L, 10L, 3L, 7L, 1L, 9L, 10L, 8L, 8L, 11L, 
6L, 9L)), .Names = c("Date", "Destination", "Categorie", "Portion_Longueur"
), row.names = c(NA, -521L), class = "data.frame")

如何强制显示所有图例,以便颜色不会改变?或者如何轻松修复每个类别的颜色以便对所有情节都一样?

1 个答案:

答案 0 :(得分:2)

您可以设置自定义色标并将其添加到您的绘图中。在循环之前,您可以添加如下内容:

cat.colors <- c("green", "blue",    "pink", "yellow") # assign a color for each level of your factor variable Categorie
names(cat.colors) <- levels(mydata$Categorie)
mycolourscale <- scale_fill_manual(name = "Categorie",values = cat.colors )

然后添加:

      + mycolourscale 

到您的ggplot代码,着色将在图表中保持一致