Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2:组合图的匹配图区域高度_R_Ggplot2 - Fatal编程技术网

R ggplot2:组合图的匹配图区域高度

R ggplot2:组合图的匹配图区域高度,r,ggplot2,R,Ggplot2,但是,这里的绘图区域高度非常不同。因此,我手动将所有绘图的高度设置为第一个 #using loops to generalise to n plots plist <- list() plist[[1]] <- p1 plist[[2]] <- p2 grobs <- list() for (i in 1:length(plist)){ if(i!=1) plist[[i]] <- plist[[i]]+theme(legend.position="none"

但是,这里的绘图区域高度非常不同。因此,我手动将所有绘图的高度设置为第一个

#using loops to generalise to n plots
plist <- list()
plist[[1]] <- p1
plist[[2]] <- p2
grobs <- list()

for (i in 1:length(plist)){
  if(i!=1) plist[[i]] <- plist[[i]]+theme(legend.position="none",plot.title=element_blank())
  grobs[[i]] <- ggplotGrob(plist[[i]])
  widths[[i]] <- grobs[[i]]$widths[2:5]
}

# fix widths
maxwidth <- do.call(grid::unit.pmax, widths)
for (i in 1:length(grobs)){
  grobs[[i]]$widths[2:5] <- as.list(maxwidth)
}

#plot
pgrob = do.call("arrangeGrob",grobs)
grid.arrange(pgrob)
for(1中的i:长度(grobs)){

grobs[[i]]$heights[2:5]您可以使用
ggpubr
包为两个绘图设置相同的图例(无需提取grobs并调整高度)


您可以使用
ggpubr
package为两个绘图设置相同的图例(无需提取网格和调整高度)


如果您可以重新构造和绑定数据帧,那么您可以使用
facet\u wrap(…,scale=“free”)
来获取所需内容

df1 <- data.frame(x=c("A1","A2","A3","A4"),something=c(10,18,24,32),col=rep(c("A","B"),2))
df2 <- data.frame(x=c("C1","C2","C3","C4"),somethingelse=c(10543,182334,242334,32255),col=rep(c("A","B"),2))

library(ggplot2)
p1 <- ggplot(df1,aes(x,something,fill=col))+
  geom_bar(stat="identity")+
  theme(legend.position="top",
        legend.justification="right",
        legend.direction="horizontal")
p2 <- ggplot(df2,aes(x,somethingelse,fill=col))+
  geom_bar(stat="identity")+
  theme(legend.position="top",
        legend.justification="right",
        legend.direction="horizontal")
库(ggplot2)
图书馆(gridExtra)

df1如果您可以重新构造和绑定数据帧,您可以使用
facet\u wrap(…,scale=“free”)
来获得您想要的

df1 <- data.frame(x=c("A1","A2","A3","A4"),something=c(10,18,24,32),col=rep(c("A","B"),2))
df2 <- data.frame(x=c("C1","C2","C3","C4"),somethingelse=c(10543,182334,242334,32255),col=rep(c("A","B"),2))

library(ggplot2)
p1 <- ggplot(df1,aes(x,something,fill=col))+
  geom_bar(stat="identity")+
  theme(legend.position="top",
        legend.justification="right",
        legend.direction="horizontal")
p2 <- ggplot(df2,aes(x,somethingelse,fill=col))+
  geom_bar(stat="identity")+
  theme(legend.position="top",
        legend.justification="right",
        legend.direction="horizontal")
库(ggplot2)
图书馆(gridExtra)

df1我真的建议坚持使用
ggpubr
。这对于自定义发布质量图(对齐图、图例等)非常有用。我真的建议坚持使用
ggpubr
。这对于自定义发布质量图(对齐图、图例等)非常有用
library(ggpubr)
figure <- ggarrange(p1, p2, nrow = 2, align = "v", common.legend = TRUE)
annotate_figure(figure, fig.lab = "Plot")
df1 <- data.frame(x=c("A1","A2","A3","A4"),something=c(10,18,24,32),col=rep(c("A","B"),2))
df2 <- data.frame(x=c("C1","C2","C3","C4"),somethingelse=c(10543,182334,242334,32255),col=rep(c("A","B"),2))

library(ggplot2)
p1 <- ggplot(df1,aes(x,something,fill=col))+
  geom_bar(stat="identity")+
  theme(legend.position="top",
        legend.justification="right",
        legend.direction="horizontal")
p2 <- ggplot(df2,aes(x,somethingelse,fill=col))+
  geom_bar(stat="identity")+
  theme(legend.position="top",
        legend.justification="right",
        legend.direction="horizontal")
library(ggplot2)
library(gridExtra)

df1 <- data.frame(x=c("A1","A2","A3","A4"),y=c(10,18,24,32),col=rep(c("A","B"),2), type = "something")
df2 <- data.frame(x=c("C1","C2","C3","C4"),y=c(10543,182334,242334,32255),col=rep(c("A","B"),2), type = "somethingelse")


df <- rbind(df1, df2)

ggplot(df,aes(x,y,fill=col))+
    ggtitle("Plot")+
    geom_bar(stat="identity")+
    facet_wrap(~type, ncol = 1, scales = "free") +
    theme(legend.position="top",
          legend.justification="right",
          legend.direction="horizontal")