0

Apparently similar code generates different results when it is abstracted in a for-loop.

This is header code common to both alternatives:

library(ggplot2)
library(gridExtra)

# factor common plot format elements
o2b <- colorRampPalette(c("brown", "orange"))(4)
textsize=8
pt <- theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(), 
           panel.background=element_blank(), panel.border=element_blank(), 
           plot.title=element_blank(), plot.margin = unit(c(5.5,12,5.5,5.5), "pt"), 
           legend.background=element_blank(), legend.key=element_blank(), legend.position=c(1,1), 
           legend.justification=c(1,1), legend.text=element_text(size=textsize), legend.title=element_text(size=textsize), 
           axis.line=element_line(colour="black"), axis.text=element_text(size=textsize, colour="black"), 
           axis.title=element_text(size=textsize))

1) This is the code without a for-loop:

# fill list of plots of 4 scatterplot rank-size distributions
p <- list()
# each plot differs by setting x values for all scatterplots to the value of x in one of the scatterplots, by turn
p[[1]] <-ggplot(temp1, aes(x=rep(rank[naics_level==2], 4), y=rhh, colour=factor(naics_level))) + pt + 
  geom_point(shape=1, size=1) + scale_color_manual(values=o2b) + 
  guides(colour = guide_legend(title="Niveau de NAICS", title.position = "left", reverse=T)) +  
  labs(x="Rang des MSA", y="Diversité sectorielle basée sur l'emploi en 2015")
p[[2]] <-ggplot(temp1, aes(x=rep(rank[naics_level==3], 4), y=rhh, colour=factor(naics_level))) + pt + 
  geom_point(shape=1, size=1) + scale_color_manual(values=o2b) + 
  guides(colour = guide_legend(title="Niveau de NAICS", title.position = "left", reverse=T)) +  
  labs(x="Rang des MSA", y="Diversité sectorielle basée sur l'emploi en 2015")
p[[3]] <-ggplot(temp1, aes(x=rep(rank[naics_level==4], 4), y=rhh, colour=factor(naics_level))) + pt + 
  geom_point(shape=1, size=1) + scale_color_manual(values=o2b) + 
  guides(colour = guide_legend(title="Niveau de NAICS", title.position = "left", reverse=T)) +  
  labs(x="Rang des MSA", y="Diversité sectorielle basée sur l'emploi en 2015")
p[[4]] <-ggplot(temp1, aes(x=rep(rank[naics_level==5], 4), y=rhh, colour=factor(naics_level))) + pt + 
  geom_point(shape=1, size=1) + scale_color_manual(values=o2b) + 
  guides(colour = guide_legend(title="Niveau de NAICS", title.position = "left", reverse=T)) +  
  labs(x="Rang des MSA", y="Diversité sectorielle basée sur l'emploi en 2015")
library(gridExtra)
grid.arrange(grobs=p, nrow=2)

which results in:

enter image description here

2) This is the same code in a for-loop:

p <- list()
for (n in 1:4) {
  p[[n]] <- ggplot(temp1, aes(x=rep(rank[naics_level==n+1], 4), y=rhh, colour=factor(naics_level))) + pt + 
    geom_point(shape=1, size=1) + scale_color_manual(values=o2b) + 
    guides(colour = guide_legend(title="Niveau de NAICS", title.position = "left", reverse=T)) +  
    labs(x="Rang des MSA", y="Diversité sectorielle basée sur l'emploi en 2015")
}
grid.arrange(grobs=p, nrow=2)

which results in:

enter image description here

This time, all plots are duplicates of the 4th plot obtained by the first method. Where am I going wrong?

9
  • 1
    can you dput the data to have a reproducible example please? Commented Sep 29, 2017 at 9:17
  • 1
    Try what for (n in 1:4) print(rep(rank[naics_level==n+1], 4)) gives you. Is this what you expect it to give? Commented Sep 29, 2017 at 9:51
  • 1
    Have you tried putting x=rep(rank[naics_level==n+1] between { and p[[n]] and using aes(x=x) and what does the list p[[n]] look like after the loop Commented Sep 29, 2017 at 10:23
  • 1
    @Olivia I followed your advice and did this for (n in 1:4) { temp <- temp1; temp$rank <- with(temp1, rep(rank[naics_level==n+1], 4)); p[[n]] <- ggplot(temp, aes(x=rank, y=rhh, colour=factor(naics_level))) + ... which works like a charm! Commented Sep 29, 2017 at 10:35
  • 2
    Within the loop, try converting each ggplot object into a grob & save that to your list instead. I've answered a similar question recently here. Commented Sep 29, 2017 at 10:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.