我使用ggplot2和knitr发布带有右侧图例的散点图.图例包含在宽高比中,因此会打破图的"方形度",如默认主题中所示.当图例文本变得比"a"和"b"稍长时,图形变为"长矩形"而不是"正方形".
我想保持图形"平方",因此我想在图表中从纵横比中排除图例ggplot2.我.Rprofile有以下信息强制ggplot2生成具有更大文本和轴标题周围更多空间的低色图:
theme_set(theme_bw(16))
theme_update(
axis.title.y = element_text(angle = 90, vjust = -.25),
axis.title.x = element_text(vjust = -1),
plot.margin = unit(c(1,1,1,1), "cm")
)
Run Code Online (Sandbox Code Playgroud)
我可以在这里添加任何内容以从宽高比中排除图例吗?到目前为止操作coord_equal和coord_fixed失败,有fig.width和fig.height选项.谢谢你的帮助!
编辑:删除工作示例,下面的问题用完整的示例代码回答(对于批准答案的延迟感到抱歉).
设置coord_fixed()应该做的诀窍:
library(ggplot2)
library(gridExtra) ## for grid.arrange()
## Create some data with one longer label
cars <- transform(mtcars,
cyl = factor(cyl, labels=c("4","6","8 is big")))
## Compute ratio needed to make the figure square
## (For a taller narrow plot, multiply ratio by number > 1;
## for a squatter plot, multiply it by number in the interval (0,1))
ratio <- with(cars, diff(range(mpg))/diff(range(wt)))
## Make plots with and without a legend
a <- ggplot(cars, aes(mpg, wt)) +
geom_point() + coord_fixed(ratio=ratio)
b <- ggplot(cars, aes(mpg, wt, color=cyl)) +
geom_point() + coord_fixed(ratio=ratio)
## Plot them to confirm that coord_fixed does fix the aspect ratio
grid.arrange(a,b, ncol=2)
Run Code Online (Sandbox Code Playgroud)
