0

I have a data.frame with following variables:

centers.df <- data.frame(clust1 = sample(c(1,2), 50, replace=T), clust2 = sample(c(1,2), 50, replace =T), brand = sample(letters[1:4], 50, replace = T), cpal = sample(LETTERS[1:4], 50, replace = T), center = rnorm(100))

I would like to create a 2x2 plot output by using ggplot.

What I have done so far:

plot_list <- vector("list", length = sum(length(unique(centers.df$clust1)), length(unique(centers.df$clust2)))
for (pl in 1:length(plot_list)) {
 for (i in 1:length(unique(centers.df$clust1))) {
  for (j in 1:length(unique(centers.df$clust2))) {  
  yrange <- range(centers.df$center)
  plot.df<- subset(centers.df, centers.df$clust1==i & centers.df$clust2==j)
  counts<- aggregate(plot.df$center, by=list(plot.df$cpal), FUN="sum")  
  g <- ggplot(counts, aes(counts[,1], counts[,2])) + geom_bar(stat = "identity") +
  theme(axis.title.x= element_blank(), axis.title.y= element_blank(),
        axis.text.x = element_text(color="#993333", size=14, angle=90),
        axis.line = element_line(colour = "darkblue",size = 1, linetype = "solid")) +
        ggtitle(paste0("Cluster ",i, ",", j, " #PoS ",length(res.som$Clusters[[i,j]])))
plot_list[[pl]] <- g 
  }
 } 
}

# grid.arrange(grobs=plot_list, nrow=2, ncol=2)

What I would like to have:

 par(mfrow = c(length(unique(centers.df$clust1)), length(unique(centers.df$clust2))))
for (i in 1:length(unique(centers.df$clust1))) {
  for (j in 1:length(unique(centers.df$clust2))) {
   yrange <- range(centers.df$center)
   plot.df<- subset(centers.df, centers.df$clust1==i & centers.df$clust2==j)
   counts<- aggregate(plot.df$center, by=list(plot.df$cpal), FUN="sum")
   barplot(height = counts[,2], las=2, cex.names = 0.7, col= "darkblue", ylim=yrange, #yaxt="n",
        names.arg=paste((as.character(counts[,1]))), 
        main=paste0("Cluster ",i, ",", j, " #PoS ",length(res.som$Clusters[[i,j]]))) 
 }
}  

Rplot01

1 Answer 1

1

Your issue is't the plotting, its the manipulation of your data before the plotting. I can't exactly work out what you're trying to do, but here's my attempt. It looks different to yours but I can't work out what you are trying to plot. In general, to plot across a grid, you can gather your variables to a tidy format (1 observation per row) then use faceting.

library(tidyverse)

centers.df <- data.frame(clust1 = sample(c(1,2), 50, replace=T),
                         clust2 = sample(c(1,2), 50, replace =T),
                         brand = sample(letters[1:4], 50, replace = T),
                         cpal = sample(LETTERS[1:4], 50, replace = T),
                         center = rnorm(100))

centers.df <- centers.df %>% 
  gather(clust, num, clust1:clust2) %>% 
  group_by(clust, num, cpal) %>% 
  summarise(center_count = sum(center))


ggplot(centers.df, aes(x = cpal, y = center_count, fill = cpal)) + 
  geom_col() + 
  facet_grid(clust~num)

Plot

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Jack! I will try this one out!

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.