I'm producing a rather wide image, listing consecutive training sessions of target practice.
At present, this is not very helpful graphic and I will need to get the image wrapped into several lines to make data for each training set more visible. So far I've been managing this by introducing a second variable that denotes the line into which a training set belongs to. The draw back is that now I have a strip above each row that serves little to no purpose.
Another way of doing this would be to generate each line as a seperate figure and merge them using gridExtra
's grid.arrange
.
I'm looking for other (elegant) ways of wrapping a wide figure to multiple rows. I would like to have no strip above each facet, if possible. All suggestions welcome.
This is the code I used to generate these images.
library(ggplot2)
library(reshape2)
st <- 23 # number of training sets
sk <- 10 # number of shots per training set
dt <- sample(7:10, size = st * sk, prob = c(0.005, 0.15, 0.195, 0.65), replace = TRUE)
rslt <- as.data.frame(matrix(dt, nrow = 10))
names(rslt) <- paste("trening", seq_len(ncol(rslt)), sep = "")
rsltm <- melt(rslt, variable.name = "trening", value.name = "rezultat")
rsltm$trening <- ordered(rsltm$trening)
n <- 5 # number of trainings per row
st.vrst <- round(st/n)
vrsta <- paste("vrsta", seq_len(st.vrst), sep = "")
rsltm$vrsta <- rep(vrsta, each = sk * n, length.out = nrow(rsltm))
rm(vrsta)
# wide figure
ggplot(rsltm, aes(x = trening, y = rezultat)) +
geom_dotplot(binaxis = "y", stackdir = "center", show_guide = FALSE) +
scale_fill_manual(values = rep("red", st)) +
theme_bw()
# wrapped figure
ggplot(rsltm, aes(x = trening, y = rezultat, fill = trening)) +
geom_dotplot(binaxis = "y", stackdir = "center", show_guide = FALSE) +
scale_fill_manual(values = rep("red", st)) +
facet_wrap(~vrsta, nrow = st.vrst, scales = "free_x") +
theme_bw()
To remove strip texts above the small plots you can set elemnt_blank()
for those elements.
# wrapped figure
ggplot(rsltm, aes(x = trening, y = rezultat, fill = trening)) +
geom_dotplot(binaxis = "y", stackdir = "center", show_guide = FALSE) +
scale_fill_manual(values = rep("red", st)) +
facet_wrap(~vrsta, nrow = st.vrst, scales = "free_x") +
theme_bw() + theme(strip.text=element_blank(),
strip.background=element_blank())
One way to improve plot could be placing of trening
labels inside the plot region.
For this purpose new data frame df.names
is made. Number of rows correspond to levels of trening
.
df.names<-data.frame(lab.nam=unique(rsltm$trening), #labels
x=rep(1:5, times = 5, length.out = 23), #x coordinate (1:5)
y=rep(11,23), #y coordinate (11)
vrsta=rep(vrsta, each = 5, length.out = 23)) #vrsta values to ensure position in facets
Code to produce plot without x axis labels. panel.margin=
ensures that space between facets is smaller.
p <- ggplot(rsltm, aes(x = trening, y = rezultat)) +
geom_dotplot(binaxis = "y", stackdir = "center", show_guide = FALSE) +
scale_fill_manual(values = rep("red", st)) +
facet_wrap(~vrsta, nrow = st.vrst, scales = "free_x") +
theme_bw() + theme(strip.text=element_blank(),
strip.background=element_blank(),
panel.margin = unit(-0.6, "lines"),
panel.grid.minor=element_blank()) +
scale_y_continuous(limits=c(7.5,11.5),breaks=c(8,9,10)) +
scale_x_discrete(breaks=NULL)
With geom_text()
place labels above dots.
p + geom_text(data=df.names,aes(x, y, label=lab.nam),size=4)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With