I'd like to add a summary plot at the end of my facet_wrap plot, of what the plot would look like if I hadn't employed the facet_wrap. For example:
test=data.frame(x=runif(1000,min=-1,max=1),y=as.character(sample(x=1:8, size=1000, replace=TRUE)))
ggplot(test, aes(x=x, fill=x)) +
geom_histogram(show.legend = FALSE)+
theme_minimal()+
facet_wrap(~y)
This produces this plot:
However, since I have that extra corner in the lower right, I'd like to add a summary plot of all the data, as if I hadn't employed variable y. It would look like this:
ggplot(test, aes(x=x, fill=x)) +
geom_histogram(show.legend = FALSE)+
theme_minimal()+
ggtitle('All Data')
Is there a way to do this, perhaps with grid.arrange?
Cheers!
Edited to adjust the test data to better represent the real data:
Note, with fake data the following code works, but not with my real data.
bins <- 200
cols <- c("#0072B2",'#253f4b',"#0072B2","#D55E00","#F0E442","#D55E00")
colGradient <- colorRampPalette(cols)
cut.cols <- colGradient(bins)
cuts <- cut
ggplot(test, aes(x=x, fill=cut(x,bins))) +
geom_histogram(show.legend = FALSE, binwidth = 0.06)+
geom_histogram(show.legend = FALSE,
data = ~ transform(.x, y = "All Data")
) +
scale_color_manual(values=cut.cols,labels=levels(cuts)) +
scale_fill_manual(values=cut.cols,labels=levels(cuts)) +
scale_x_continuous(breaks=c(-1,0,1), labels=c('dusk', 'dawn','dusk'), limits = c(-1.1,1.1))+
theme_minimal()+
facet_wrap(~y)
normalizedtime==x, station==y
One option to achieve your desired would be to use a second geom_histogram
like so:
library(ggplot2)
set.seed(123)
ggplot(test, aes(x = x)) +
geom_histogram() +
geom_histogram(
data = ~ transform(.x, y = "All Data")
) +
theme_minimal() +
facet_wrap(~y)
UPDATE Here is the adapted code based on the data you provided via dput
. Still puzzled what's the reason for the error you got when you applied the code to your data. Note: I adapted the code to put the "All Data"
category last. Additionally, I used a different approach to color the bars using after_stat()
.
library(ggplot2)
DPHdol$station <- factor(
DPHdol$station,
levels = c(unique(DPHdol$station), "All Data")
)
bins <- 100
cols <- c("#0072B2", "#253f4b", "#0072B2", "#D55E00", "#F0E442", "#D55E00")
colGradient <- colorRampPalette(cols)
cut.cols <- colGradient(bins)
cuts <- cut
ggplot(DPHdol, aes(x = normalizedtime)) +
geom_histogram(
aes(fill = after_stat(factor(x))),
bins = bins
) +
geom_histogram(
aes(fill = after_stat(factor(x))),
data = ~ transform(.x, station = factor("All Data", levels = levels(station))),
bins = bins
) +
scale_color_manual(values = cut.cols, labels = levels(cuts)) +
scale_fill_manual(values = cut.cols, labels = levels(cuts)) +
scale_x_continuous(
breaks = c(-1, 0, 1),
labels = c("dusk", "dawn", "dusk"),
limits = c(-1.1, 1.1)
) +
theme_minimal() +
facet_wrap(~station) +
guides(fill = "none")