Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: How to move x-axis up, so it can be under arbitrarily picked bar on horizontal barplot?

Tags:

plot

r

ggplot2

I made horizontal barplot. I need to move x-axis up, so it is placed not under the last bar, but under some bar, picked based on other criterion. I've tried some things, like gtable, but with no success. I would appreciate any help.

This is a picture that illustrats what I want to achieve:

enter image description here

Here is the code to produce sample horizontal barplot:

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)
like image 872
ltw Avatar asked Nov 08 '22 19:11

ltw


1 Answers

You could create two separate plots first, removing the axis ticks and labels in one of them entirely:

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) 

Then you can use plot_grid from package cowplot to arrange the two plots, with align="h" aligning both plots horizontally:

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

enter image description here

like image 55
erc Avatar answered Nov 15 '22 08:11

erc