R 将ggplot plot设置为具有相同的x轴宽度和点打印行之间的相同间距
更新了问题,纳入了已在SO上回答的部分解决方案 我正在使用R 将ggplot plot设置为具有相同的x轴宽度和点打印行之间的相同间距,r,ggplot2,gridextra,R,Ggplot2,Gridextra,更新了问题,纳入了已在SO上回答的部分解决方案 我正在使用ggplot2创建多个绘图,并使用gridExtra将绘图合并为一个图形和多个面板,全部位于一列中。我的问题是,我无法使点打印行之间的空间在两个打印中保持一致 library(ggplot2) # data dat1 <- data.frame(VARIABLES=c("Item 1", "Item 2 is a little longer"), est=c(.3, .5),
ggplot2
创建多个绘图,并使用gridExtra
将绘图合并为一个图形和多个面板,全部位于一列中。我的问题是,我无法使点打印行之间的空间在两个打印中保持一致
library(ggplot2)
# data
dat1 <- data.frame(VARIABLES=c("Item 1", "Item 2 is a little longer"),
est=c(.3, .5),
min=c(.2, .4),
max=c(.4, .7))
dat2 <- data.frame(VARIABLES=c("Item 3",
"Item 4 is even longer if you can believe it",
"And there is a third item",
"And a fourth item"),
est=c(.3, .5, .3, .5),
min=c(.2, .4, .2, .4),
max=c(.4, .7, .4, .7))
dat <- c("dat1", "dat2")
labs <- c("Plot 1", "Plot2")
# create plots
count <- 1
for (i in dat) {
p <- ggplot(get(i), aes(x=reorder(as.character(VARIABLES), est),
y=est)) +
geom_pointrange(aes(ymin=min,
ymax=max),
linetype="dashed") +
geom_point(size=3) +
ylim(-1,1) +
theme_bw() +
labs(title = labs[count]) +
theme(legend.position="none") +
coord_flip()
assign(paste(i, "plot", sep="."), p)
count <- count+1
}
# combine plots
library(gridExtra)
# approach suggested by @baptise
# http://stackoverflow.com/questions/13294952/left-align-two-graph-edges-ggplot
gA <- ggplotGrob(dat1.plot)
gB <- ggplotGrob(dat2.plot)
maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
gA$widths[2:5] <- as.list(maxWidth)
gB$widths[2:5] <- as.list(maxWidth)
grid.arrange(gA, gB, ncol=1)
库(ggplot2)
#资料
dat1这里有一种方法:为较短的标签填充额外的空格,并使用单空格字体
longest_name <- max(nchar(as.character(dat1$VARIABLES)), nchar(as.character(dat2$VARIABLES)))
fill_in_spaces <- function(v) paste0(paste0(rep(" ", longest_name - nchar(v)), collapse=""), v)
levels(dat1$VARIABLES) <- sapply(levels(dat1$VARIABLES), fill_in_spaces)
levels(dat2$VARIABLES) <- sapply(levels(dat2$VARIABLES), fill_in_spaces)
longest\u namedat1$Plotlibrary(gridExtra)
图书馆(网格)
gb1我打算建议使用面
而不是网格。排列
,但“ggplot2目前不支持带有非笛卡尔坐标或坐标翻转的自由缩放。”@shadow,是的,我很失望地了解到这一点。我最初认为facets可能是答案。我认为#1主要是@baptiste的变体,在这种情况下什么是合适的,因为(1)在另一个问题中得到了回答,而不是(2)?我是否应该编辑该问题以纳入(1)的其他答案,并澄清我的问题涉及(2)?我不知道。我的猜测是,最好一开始每个问题只有一个特定的问题。继续编辑,我已经取消了我的最后一票。解决问题的有趣方法#2@tonytonov@EricGreen谢谢,这是一个完全随机的想法,它(令人惊讶地)成功了。facet\u grid(Plot~,scales=“free”,space=“free”)
将解决这个问题,看起来它会成功的。谢谢但是,我还要问g,@baptiste,是否可以修改代码来指定行之间的特定空间?您的代码似乎完美地回答了这个问题。只是想知道是否有可能指定一个可以应用于所有绘图的特定间距。我已经更新以适应多个绘图。绘图间距由绘图边距定义;或者,您可以查看gtable文档中的?gtable\u add_rows
和类似函数。通过为我的另外3个绘图添加参数,我能够使答案中的代码在多个绘图中完美工作:gb3…gb5,n3…n5,gC…gE
和g1我不知道,但我认为今天以下语法已经改变了:n1
p <- p + theme(text=element_text(family="Courier", size=14))
dat1$Plot <- "Plot 1"
dat2$Plot <- "Plot 2"
dataset <- rbind(dat1, dat2)
ggplot(
dataset,
aes(
y = reorder(as.character(VARIABLES), est),
x = est,
xmin = min,
xmax = max
)) +
geom_errorbarh() + geom_point() +
facet_wrap(~Plot, ncol = 1, scales = "free_y")
library(gridExtra)
library(grid)
gb1 <- ggplot_build(dat1.plot)
gb2 <- ggplot_build(dat2.plot)
# work out how many y breaks for each plot
n1 <- length(gb1$layout$panel_params[[1]]$y.labels)
n2 <- length(gb2$layout$panel_params[[1]]$y.labels)
gA <- ggplot_gtable(gb1)
gB <- ggplot_gtable(gb2)
g <- rbind(gA, gB)
# locate the panels in the gtable layout
panels <- g$layout$t[grepl("panel", g$layout$name)]
# assign new (relative) heights to the panels, based on the number of breaks
g$heights[panels] <- unit(c(n1,n2),"null")
grid.newpage()
grid.draw(g)