Issue with palette and ggplot inside a loop

155 views
Skip to first unread message

Paulo Eduardo Cardoso

unread,
Aug 27, 2012, 11:50:38 AM8/27/12
to ggp...@googlegroups.com
Is there any known issue with applying a custom palette to a geometry when ggplot is built inside a loop?

I'm using a loop to subset a dataset, build a map and apply a given palette (and legend) accordingly.

People don't like loops by in my case it allows me to easily retain objects and control the flux of producing my maps since I have many different scenarios to consider.

I'm storing each resulting ggplot in a list() object. The list() will be a list of ggplot objects, one for each subset of my dataset.

The behavior is strange [for me] since I get the plots done but only colors are not applied to polygons. The values still being identified in the legend.

I don't know exactly how is works but retrieving the ggplot object, I see that it applies a function(n) to palette.

to exemplify
...
map.ij <- NULL
for (j in 1:n) {
  ...
    map.ij[[j]]  ggplot(gridij[[j]], aes(x = long, y = lat)) + # The base data is a grid with the merged dataset
    geom_polygon(aes(group = id, fill = cln), colour = 'grey80') + # 
    # scale_color_manual(values = kcolorlimitgrid)  + # not used here
    coord_equal() +
    scale_x_continuous(limits c(x1, y2)) + 
    scale_y_continuous(limits c(y1, y2)) + 
    scale_fill_manual( # polygons fill be filled using the custom palette.cln[[j]]
      name = paste('Legend', '\nSeason :', kSeason[j], sep = ''),
      values = palette.cln[[j]], # the issue is here
      drop = F) + 
    opts(
      ...
    )
}

map.ij[[n]]  is the only with color palette applied to polygons. The legend is Ok for each j map.

Any idea?

Should I post a better, reproducible example?

Brandon Hurr

unread,
Aug 28, 2012, 10:43:51 AM8/28/12
to Paulo Eduardo Cardoso, ggp...@googlegroups.com
Could you go ahead and supply a fully working example? 

On Mon, Aug 27, 2012 at 4:50 PM, Paulo Eduardo Cardoso <pauloed...@gmail.com> wrote:
gridij[[j]]

Paulo Eduardo Cardoso

unread,
Aug 28, 2012, 10:58:15 AM8/28/12
to ggp...@googlegroups.com
I'm working on this right now.

2012/8/28 Brandon Hurr <brando...@gmail.com>

Could you go ahead and supply a fully working example? 

On Mon, Aug 27, 2012 at 4:50 PM, Paulo Eduardo Cardoso <pauloed...@gmail.com> wrote:
gridij[[j]]




--
                                                          
Paulo Eduardo Cardoso                                    
Linkedin: http://pt.linkedin.com/in/pauloecardoso
Facebook: http://pt-pt.facebook.com/pecardoso
Skype: pauloeducardoso.strix

Paulo Eduardo Cardoso

unread,
Aug 30, 2012, 7:40:19 AM8/30/12
to ggp...@googlegroups.com, Paulo Eduardo Cardoso

A simple example

#Create a list of colors to be used with scale_manual 
palette.l <- list() 
palette.l[[1]] <- c('red', 'blue', 'green') 
palette.l[[2]] <- c('pink', 'blue', 'yellow') 
# Store each ggplot in a list object 
plot.l <- list() 
#Loop it 
for(i in 1:2){ 
  plot.l[[i]] <- qplot(mpg, wt, data = mtcars, colour = factor(cyl)) + 
    scale_colour_manual(values = palette.l[[i]]) 
} 

In my session plot.l[1] will be painted with colors from palette.l[2].

Brandon Hurr

unread,
Aug 30, 2012, 8:19:56 AM8/30/12
to Paulo Eduardo Cardoso, ggp...@googlegroups.com
Yeah, it's not working for me either. sessionInfo() at bottom.

#Out of a loop

#works plot 1
ggplot(data= mtcars, aes(x=wt, y=mpg, colour = factor(cyl))) +
geom_point()+
scale_colour_manual(values = palette.l[[1]])

#works plot 2
ggplot(data= mtcars, aes(x=wt, y=mpg, colour = factor(cyl))) +
geom_point()+ 
scale_colour_manual(values = palette.l[[2]])


#But, in a loop, both plots have palette.l[[2]] colors... 

for(i in 1:2){
plot.l[[i]] <- ggplot(data= mtcars, aes(x=wt, y=mpg, colour = factor(cyl))) +
geom_point()+


scale_colour_manual(values = palette.l[[i]])
}

#Strangely, this works, and gives palette.l[[1]] colors for both plots...
for(i in 1:2){
plot.l[[i]] <- ggplot(data= mtcars, aes(x=wt, y=mpg, colour = factor(cyl))) +
geom_point()+
scale_colour_manual(values = palette.l[[i-1]])
}

#I worry that this is a deeper problem than ggplot... 

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plotrix_3.4-1  reshape2_1.2.1 plyr_1.7.1     ggplot2_0.9.1 

loaded via a namespace (and not attached):
 [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2       grid_2.15.1        labeling_0.1       MASS_7.3-18       
 [7] memoise_0.1        munsell_0.3        proto_0.3-9.2      RColorBrewer_1.0-5 scales_0.2.1       stringr_0.6.1   

Paulo Eduardo Cardoso

unread,
Aug 30, 2012, 8:37:21 AM8/30/12
to Brandon Hurr, ggp...@googlegroups.com
I think its a problem with palette function call in ggplot(). I'll not call it a bug yet.

2012/8/30 Brandon Hurr <brando...@gmail.com>

Dennis Murphy

unread,
Aug 30, 2012, 11:43:47 AM8/30/12
to Paulo Eduardo Cardoso, ggp...@googlegroups.com
Hi:

I accidentally got this to work while playing with browser(). If you pass the argument into the environment of the function before passing it to scale_colour_manual(), it seems to work. 

palette.l <- list() 
palette.l[[1]] <- c('red', 'blue', 'green') 
palette.l[[2]] <- c('pink', 'blue', 'yellow') 

plotfun <- function(x) {
    require('ggplot2')
    pal <- x
   qplot(mpg, wt, data = mtcars, colour = factor(cyl)) +
      scale_colour_manual(values = pal)
     }
plot.l <- lapply(palette.l, plotfun)

plot.l[[1]]
plot.l[[2]]

Dennis

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Aug 30, 2012, 12:05:45 PM8/30/12
to Dennis Murphy, Paulo Eduardo Cardoso, ggp...@googlegroups.com
So what was going on internally? 

That was freaking weird. 

B

Winston Chang

unread,
Aug 30, 2012, 8:11:11 PM8/30/12
to Brandon Hurr, Dennis Murphy, Paulo Eduardo Cardoso, ggp...@googlegroups.com
This is kind of weird... In the example below, pals[[i]] isn't
evaluated until plot is rendered:

# Create 3 palettes
pals <- list(
c('red', 'blue', 'green'),
c('pink', 'blue', 'yellow'),
c("white", "grey50", "black"))

# Make 2 plots
plots <- list()
for(i in 1:2){
plots[[i]] <- ggplot(data= mtcars, aes(x=wt, y=mpg, colour = factor(cyl))) +
geom_point() +
scale_colour_manual(values = pals[[i]])
}

# Set i to 3
i <- 3
# Renders with pals[[3]], although we used pals[[i]] above
print(plots[[1]])
print(plots[[2]])


Here's what I think is happening: the scales aren't "trained" until
ggplot_build() is called, and that happens in print.ggplot(), not in
ggplot().

It's possible to make it work by calling plot_build explicitly, but
this probably isn't a good long-term solution.

# Make 2 plots
plots <- list()
for(i in 1:2){
plots[[i]] <- ggplot_build(ggplot(data= mtcars, aes(x=wt, y=mpg,
colour = factor(cyl))) +
geom_point() +
scale_colour_manual(values = pals[[i]]))
}

# Set i to 3
i <- 3
# Renders with pals[[1]] and pals[[2]]
print(plots[[1]]$plot)
print(plots[[2]]$plot)


-Winston
Reply all
Reply to author
Forward
0 new messages