Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrap a wide image into several lines

I'm producing a rather wide image, listing consecutive training sessions of target practice.

enter image description here

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.

enter image description here

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()
like image 346
Roman Luštrik Avatar asked Oct 05 '22 11:10

Roman Luštrik


1 Answers

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())

EDIT - update of produced plot

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)

enter image description here

like image 162
Didzis Elferts Avatar answered Oct 10 '22 02:10

Didzis Elferts