Ggplot2: How to move the x axis up, so that it can be below an arbitrarily selected bar on a horizontal barplot?

I made a horizontal stroke. I need to move the x axis up, so it is not placed under the last strip, but under some panel selected by another criterion. I tried some things like gtable, but without success. I would appreciate any help.

This is an image that illustrates what I want to achieve:

enter image description here

Here is the code to create a horizontal sample barrel:

library("ggplot2")
library("RColorBrewer")
colours <- brewer.pal(11, "RdYlGn")[3:9]

no.names <- 4

name.percentage <- data.frame(name = paste0(LETTERS[1:no.names], letters[1:no.names], sample(LETTERS[1:no.names], size = no.names, replace = TRUE )), percentage = 0.85 + runif(no.names, 0, 0.15))

name.percentage <- rbind( 
transform(name.percentage, type = 1, fill = cut(percentage, breaks = c(-Inf,(1:6 * 3 + 81)/100, Inf), right = T, labels = colours)),
transform(name.percentage, percentage = 1 - percentage, type = 2, fill = "#EEEEEE")
)


plot <- ggplot(data = name.percentage, 
     aes( x = name, y = percentage, fill = fill)) +
geom_bar(stat = "identity", position = "stack", width = 0.75) + 
scale_fill_identity(guide = "none")  + 
labs(x = NULL, y = NULL) + 
scale_y_continuous(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0)) +
coord_flip() +
theme_classic() +
theme(axis.ticks.y = element_blank(),
      axis.text.y = element_text(size = 11, colour = "black" ),
      axis.text.x = element_text(size = 11, colour = "black" ),
      axis.line = element_blank(),
      plot.margin = grid::unit(c(5,5,5,5),"mm"),
      aspect.ratio = ((no.names %% 30) / 30 ) * 1.70) 

print(plot)
+4
source share
1 answer

First, you can create two separate graphs, completely removing ticks and axis labels in one of them:

plot1 <- ggplot(data = subset(name.percentage, name=="AaC" | name=="BbA"), 
               aes( x = name, y = percentage, fill = fill)) +
  geom_bar(stat = "identity", position = "stack", width = 0.75) + 
  scale_fill_identity(guide = "none")  + 
  labs(x = NULL, y = NULL) + 
  scale_y_continuous(expand = c(0,0)) +
  scale_x_discrete(expand = c(0,0)) +
  coord_flip() +
  theme_classic() +
  theme(axis.ticks.y = element_blank(),
        axis.text.y = element_text(size = 11, colour = "black" ),
        axis.text.x = element_blank(),
        axis.line=element_blank(),
        axis.ticks=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        aspect.ratio = ((no.names %% 30) / 30 ) * 1.70) 

plot2 <- ggplot(data = subset(name.percentage, name=="CcA" | name=="DdD"), 
                aes( x = name, y = percentage, fill = fill)) +
  geom_bar(stat = "identity", position = "stack", width = 0.75) + 
  scale_fill_identity(guide = "none")  + 
  labs(x = NULL, y = NULL) + 
  scale_y_continuous(expand = c(0,0)) +
  scale_x_discrete(expand = c(0,0)) +
  coord_flip() +
  theme_classic() +
  theme(axis.ticks.y = element_blank(),
        axis.text.y = element_text(size = 11, colour = "black" ),
        axis.text.x = element_text(size = 11, colour = "black" ),
        axis.line = element_blank(),
        aspect.ratio = ((no.names %% 30) / 30 ) * 1.70) 

plot_grid cowplot , align="h" :

library(cowplot)
plot_grid(plot2, plot1, align="h", ncol=1)

enter image description here

+1

All Articles