0

I'm plotting data as follow and export image in tiff format through a loop:

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

}

The problem is that from a plot to another, when there's category which is not present, the legend change while I want to compare visually all the plot with the same color code.

Data :

> dput(mydata)
structure(list(Date = structure(c(11L, 1L, 9L, 9L, 10L, 1L, 2L, 
3L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 7L, 4L, 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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, 4L, 
4L, 12L, 3L, 3L, 8L, 11L, 1L, 11L, 11L, 11L, 10L, 4L, 6L, 12L, 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 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")

how to force to show all the legend so the color won't change ? or how to fix color for each category easily in order to be the same for all plot ?

ranell
  • 683
  • 13
  • 29
  • The key is coding `Categorie` as a factor and having the same factor levels for `Categorie` in both data frames, regardless of whether either data frame has all of the levels present. See [this SO answer](http://stackoverflow.com/a/34950563/496488) for an example that implements this solution. – eipi10 Mar 01 '16 at 23:05

1 Answers1

2

You can set up a custom color scale and add it to your plot. Before your loop you could add something like the following:

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 )

Then add:

      + mycolourscale 

to your ggplot code and the coloring will be consistent across plots

Wyldsoul
  • 1,468
  • 10
  • 16
  • Unfortunately still the same issue : Please see the Edit. Thanks a lot. – ranell Mar 01 '16 at 22:35
  • As epi10 said above, Categorie must be coded as a factor variable in the original data.frame, before you subset it in the loop. Then the above code (modified so that you have a color for each factor) should work. – Wyldsoul Mar 02 '16 at 13:16
  • I'm yet stack, i'm loading csv in mydata, and when I try names(cat.colors) <- levels(mydata$Categorie), it turns null, is it normal ? – ranell Mar 02 '16 at 13:22
  • It would be easier to isolate your issue if you posted some sample data. Try dput(mydata), or if its large, dput(head(mydata, 10)), and copy the results to an edit of your original post. – Wyldsoul Mar 02 '16 at 13:28
  • I've put all datas because with 10 lines the problem won't appears, i've also put all the code, even it is long, maybe the problem come from other functions. Thanks a lot for your help. – ranell Mar 02 '16 at 13:53
  • 1
    Add this code above the start of your loop: cat.colors <- c("green", "blue", "red") names(cat.colors) <- c("1", "2", "3") mycolourscale <- scale_fill_manual(name = "Categorie",values = cat.colors ). and then add + mycolourscale after your geom_bar in the ggplot code – Wyldsoul Mar 02 '16 at 17:09
  • Thanks you very much , finally it work ! you assigned levels because my table do not return any one, but why it does not ? – ranell Mar 02 '16 at 17:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105171/discussion-between-wyldsoul-and-ranell). – Wyldsoul Mar 02 '16 at 17:17