0

I have a small issue with geom_col and facet_grid. I want to plot the percentage of active and resting individuals that received an x amount of pokes (ranging from 1 to 4, see x axis) during an experiment. I would like to keep the same position of the columns throughout the different panes, meaning the column of the active ones on the left (dark grey) and the column of the resting one on the right (light grey). In this way, each value of Round.no would have room "reserved" for the column of active individuals, even if there are no active individuals that received that number of pokes in that round.

As you can see from the image below, ggplot is moving the columns to the left, where there is free room (no active individuals that received that amount of pokes in that round). This is the case of the individuals that receved 3 pokes in the first panel and 4 pokes in the third panel.

enter image description here

How can I maintain the order, without changing the order of the factors? I need to maintain this order (active left, resting right) for coherence with other plots.

Here's the code I have been using:

plot <- ggplot(plot_summary.df, aes(x = as.factor(no.Pokes), y = perc, fill = Resting)) 
plot <- plot + geom_col(position = position_dodge(preserve = "single"))
plot <- plot + facet_grid(rows = plot_summary.df$Round.no)

Thank you

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

0

I've answered myself seconds after posting this...

All that it is needed is to "fill" the emtpy spaces with a percentage = 0. I have created 4 rows in which the perc value equals to 0. Therefore, ggplot account for those columns but does not show them.

Code attached:

dumdum <- plot_summary.df[1:4,]

dumdum$Round.no <- c(1, 1, 1, 3)
dumdum$no.Pokes <- c(3, 4, 4, 4)
dumdum$Resting<- c("N", "N", "Y", "N")
dumdum$perc <- c(0, 0, 0, 0)

plot_summary.df <- bind_rows(plot_summary.df, dumdum)

Result:

enter image description here