当前位置 : 主页 > 手机开发 > 无线 >

ggplot2:如何向上移动x轴,所以它可以在水平条形图下任意选取条形图?

来源:互联网 收集:自由互联 发布时间:2021-06-10
我做了水平画布.我需要向上移动x轴,所以它不是放在最后一个条下面,而是放在某个条形图下,根据其他标准选择. 我尝试了一些东西,比如gtable,但没有成功.我将不胜感激任何帮助. 这张照
我做了水平画布.我需要向上移动x轴,所以它不是放在最后一个条下面,而是放在某个条形图下,根据其他标准选择.
我尝试了一些东西,比如gtable,但没有成功.我将不胜感激任何帮助.

这张照片说明了我想要实现的目标:

enter image description here

以下是生成样本水平条形图的代码:

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)
您可以先创建两个单独的图,完全删除其中一个中的轴刻度和标签:

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来排列两个图,其中align =“h”水平对齐两个图:

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

enter image description here

网友评论