I am following up on the question and problem in this example:
R stacked barchart with aggregate data
To recap, here is the original problem and the rather elegant solution.
D <- as.data.frame(structure(list(Group = c("A", "A", "A", "A", "A",
"A", "B", "B", "B", "B", "B", "B"
), Education = c("NVQ Level 4 and above", "NVQ Level3", "NVQ Level 2",
"Below NVQ Level 2", "Other qualification", "No qualification",
"NVQ Level 4 and above", "NVQ Level3", "NVQ Level 2", "Below NVQ Level 2",
"Other qualification", "No qualification"), Full.Time = c(47,
27, 23, 17, 18, 9, 36, 26, 22, 22, 27, 12), PT.16.hours = c(20,
24, 22, 18, 18, 12, 22, 21, 21, 22, 14, 10), PT.16.hours.1 = c(12,
11, 10, 11, 13, 5, 24, 25, 25, 20, 16, 12)), .Names = c("Group",
"Education", "Full.Time", "PT>16.hours", "PT<16.hours")))
The solution is to plot a stacked barchart with the two types of Education (as the facet) is as follows:
library(reshape2)
library(ggplot2)
df <- melt(D)
ggplot(df, aes(x = Education, y = value, fill = variable)) +
geom_bar(stat = "identity") +
facet_wrap(~ Group)
However, this leads to a faceted situation. I was wondering how I can have a situation with the two barcharts side by side but without the faceting but an overarching label spanning the six columns of education with the group label (in this case A or B).
I can do the following:
df <- data.frame(df, groupsandeducation = interaction(df$Group, df$Education))
ggplot(df, aes(x = groupsandeducation, y = value, fill = variable)) +
geom_bar(stat = "identity") +
ylab('') +
xlab('') +
labs(title = '') +
scale_fill_discrete('') +
theme_bw() +
theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))
This gives me the following figure:
Which gets me part of the way. However, I would like to have the following additionally happen.
- A half-bar widths's worth of space perhaps between the two sets of barcharts.
- The labels on the x-axis markings should have the A. and the B. stripped out.
- I would like A to spand the first six bars and B to span the last six bars on the x-axis.
I want it all in a single figure, hence the desire not to have a facet. How does one do this?
[Updated with solution:] Thanks, here is my solution.
out <- melt(D) %>%
arrange(Group, Education) %>%
mutate(position = rep(c(seq(from = 0.8, by = 0.4, length = 6), 0.8 + 0.4*5-0.2 + seq(from = 0.8, by = 0.4, length = 6)),
each = 3))
ggplot(out, aes(x = position, y = value, fill = variable)) +
geom_bar(stat = "identity", size = 0.5) +
scale_x_continuous(name = "Group", breaks = c(1.8, 4.4), labels = unique(out$Group)) + ylim(c(-20,100)) +
annotate(geom = "text",
x = unique(out$position), y = 0,
label = rep(as.character(out$Education[3*(1:6)]), times = 2), size = 3, angle = 90, hjust = 1) + theme_light()
[Updated:] I am trying to use coord_polar on this in order to reproduce the circular stacked barchart as in this example.
with code on github
The function is too complicated for my actual problem and does not have an explanation for why it does what it does anywhere because it is uncommented code written by an expert.