0
votes

I have been working through different iterations of this, but keep coming up short. I am hoping for some guidance on how I can split my dataframe into 4 equal groups(based on totals for each variable).

After that, I would like to visualize each group into a stacked bar chart. I am new to this, so any input would help a lot.

Example:

library(ggplot2)
library(plyr)

list <- list(BOS= 134,CUN=205,FLL= 114,HNL =  95, LAS = 288, LAX = 218, 
LGA = 456, LGW = 169, MBJ = 71, MCO = 223, OGG = 99, PHX = 167, PSP = 86, 
PVR = 80, RSW = 44, SFO = 44, SNA = 196, YBR = 43, 
YDF = 93, YEG = 2773, YFC = 114, YHM = 98, YHZ = 1266, YKF = 47, YLW = 482, 
YMM = 239, YOW = 1097,YQB = 141, YQM = 80, YQQ = 102, YQR = 334, YQT = 167, 
YQU = 85, YQY = 45, YUL = 1210, YVR = 5298, 
YWG = 1257, YXE = 357, YXJ = 42, YXS = 111, YXT = 67, YXU = 130, YXX = 128, 
YYC = 7737, YYG = 55, YYJ = 335, YYT = 475, YYZ = 13283, YZF = 55)

# Data Frame

DF = as.data.frame(do.call(rbind,list))
DF$name = rownames(DF)
colnames(DF)[colnames(DF)=="V1"] = "total"

DF <- data.frame(DF$name, opsDF$total)
DF <-rename(DF, c("DF.name"="Base", "opsDF.total"="Total"))
DF <- DF[order(DF$Total,decreasing=TRUE),]

# Plot

I am trying to split this plot into 4 equal groups - according to each base total - then visualize into a new stacked bar chart.

 ggplot(DF, aes(x = reorder(Base, Total), y = Total, label = Total, fill= 
 Base, colour = Total)) + geom_bar(stat="identity")  + 
  scale_fill_hue(l=40, c=35) + ggtitle("Log Entries") + 
    geom_text(size = 5, vjust = 0.3, color = "black", angle = 0, nudge_x =0, 
     nudge_y = 200) + theme_minimal() + xlab("Base") + ylab("Entries") + 
       theme(axis.text.x = element_text(angle = 0, hjust = 0, size = 10, 
          face = "bold"), axis.title = element_text(size = 15, face = 
              "bold")) + theme(legend.position = "none") + 
          guides(fill=FALSE) + coord_flip() 

Version 1 chart

1

1 Answers

0
votes

You can use a dummy variable DF$part to generate groups which will afterwards be visualized with facets in ggplot.

n <- nrow(DF)
DF$part <- sort(rep_len(1:4, length.out = n))

These groups are then of equal size (if possible).

table(DF$part)
#  1  2  3  4 
# 13 12 12 12 

ggplot(DF, aes(x = reorder(Base, Total), y = Total, label = Total, fill= 
                 Base, colour = Total)) + geom_bar(stat="identity")  + 
  scale_fill_hue(l=40, c=35) + ggtitle("Log Entries") + 
  geom_text(size = 5, vjust = 0.3, color = "black", angle = 0, nudge_x =0, 
            nudge_y = 200) + theme_minimal() + xlab("Base") + ylab("Entries") + 
  theme(axis.text.x = element_text(angle = 0, hjust = 0, size = 10, 
                                   face = "bold"), axis.title = element_text(size = 15, face = 
                                                                               "bold")) + theme(legend.position = "none") + 
  guides(fill=FALSE) + coord_flip()  + 
## add this line to your ggplot
  facet_wrap(~part, scales = "free_y")

Have a look at the options of scales argument in facet_wrap (see ?facet_wrap) to change the behavior of the facets x and y axes.


NOTE:

you could also stack them by setting aes(x = reorder(part, Total))... However, this might end up in unreadable text annotations and box colors etc.