简体   繁体   English

如何在 ggplot2 中使轴居中

[英]How To Center Axes in ggplot2

In the following plot, which is a simple scatter plot + theme_apa(), I would like that both axes go through 0.在下面的 plot 中,这是一个简单的散点 plot + theme_apa(),我希望这两个轴 go 到 0。 在此处输入图像描述

I tried some of the solutions proposed in the answers to similar questions to that but none of them worked.我尝试了类似问题的答案中提出的一些解决方案,但没有一个奏效。

A MWE to reproduce the plot:重现 plot 的 MWE:

library(papaja)
library(ggplot2)
library(MASS)

plot_two_factor <- function(factor_sol, groups) {
  the_df <- as.data.frame(factor_sol)
  the_df$groups <- groups
  p1 <- ggplot(data = the_df, aes(x = MR1, y = MR2, color = groups)) +
    geom_point() + theme_apa()

}

set.seed(131340)
n <- 30
group1 <- mvrnorm(n, mu=c(0,0.6), Sigma = diag(c(0.01,0.01)))
group2 <- mvrnorm(n, mu=c(0.6,0), Sigma = diag(c(0.01,0.01)))
factor_sol <- rbind(group1, group2)
colnames(factor_sol) <- c("MR1", "MR2")
groups <- as.factor(rep(c(1,2), each = n))

print(plot_two_factor(factor_sol, groups))

The papaja package can be installed via木瓜package可以通过安装

devtools::install_github("crsh/papaja")

What you request cannot be achieved in ggplot2 and for a good reason, if you include axis and tick labels within the plotting area they will sooner or later overlap with points or lines representing data.在 ggplot2 中无法实现您的要求,并且有充分的理由,如果您在绘图区域内包含轴和刻度标签,它们迟早会与表示数据的点或线重叠。 I used @phiggins and @Job Nmadu answers as a starting point.我使用@phiggins 和@Job Nmadu 的答案作为起点。 I changed the order of the geoms to make sure the "data" are plotted on top of the axes.我更改了几何图形的顺序以确保“数据”绘制在轴的顶部。 I changed the theme to theme_minimal() so that axes are not drawn outside the plotting area.我将主题更改为theme_minimal()以便不在绘图区域之外绘制轴。 I modified the offsets used for the data to better demonstrate how the code works.我修改了用于数据的偏移量,以更好地演示代码的工作方式。

library(ggplot2)

iris %>% 
  ggplot(aes(Sepal.Length - 5, Sepal.Width - 2, col = Species)) +
  geom_hline(yintercept = 0) +
  geom_vline(xintercept = 0) +
  geom_point() +
  theme_minimal()

This gets as close as possible to answering the question using ggplot2.这尽可能接近使用 ggplot2 回答问题。

在此处输入图像描述

Using package 'ggpmisc' we can slightly simplify the code.使用 package 'ggpmisc' 我们可以稍微简化代码。

library(ggpmisc)

iris %>% 
  ggplot(aes(Sepal.Length - 5, Sepal.Width - 2, col = Species)) +
  geom_quadrant_lines(linetype = "solid") +
  geom_point() +
  theme_minimal()

This code produces exactly the same plot as shown above.此代码生成与上图所示完全相同的 plot。

If you want to always have the origin centered, ie, symmetrical plus and minus limits in the plots irrespective of the data range, then package 'ggpmisc' provides a simple solution with function symmetric_limits() .如果您希望始终使原点居中,即无论数据范围如何,图中的对称正负限制,然后 package 'ggpmisc' 提供了一个简单的解决方案 function symmetric_limits() This is how quadrant plots for gene expression and similar bidirectional responses are usually drawn.这就是通常绘制基因表达和类似双向反应的象限图的方式。

iris %>% 
  ggplot(aes(Sepal.Length - 5, Sepal.Width - 2, col = Species)) +
  geom_quadrant_lines(linetype = "solid") +
  geom_point() +
  scale_x_continuous(limits = symmetric_limits) +
  scale_y_continuous(limits = symmetric_limits) +
  theme_minimal()

在此处输入图像描述

The grid can be removed from the plotting area by adding + theme(panel.grid = element_blank()) after theme_minimal() to any of the three examples.可以通过在theme_minimal()之后向三个示例中的任何一个添加+ theme(panel.grid = element_blank())来从绘图区域中删除网格。

Loading 'ggpmisc' just for function symmetric_limits() is overkill, so here I show its definition, which is extremely simple:只为 function symmetric_limits()加载 'ggpmisc' 太过分了,所以我在这里展示它的定义,非常简单:

symmetric_limits <- function (x) 
{
    max <- max(abs(x))
    c(-max, max)
}

For the record, the following also works as above.作为记录,以下内容也如上所述。

iris %>%
    ggplot(aes(Sepal.Length-6.2, Sepal.Width-3.2, col = Species)) +
    geom_point() +
    geom_hline(yintercept = 0) +
    geom_vline(xintercept = 0)

Setting xlim and slim should work.设置 xlim 和 slim 应该可以。

library(tidyverse)

# default
iris %>% 
  ggplot(aes(Sepal.Length, Sepal.Width, col = Species)) +
  geom_point()


# setting xlim and ylim
iris %>% 
  ggplot(aes(Sepal.Length, Sepal.Width, col = Species)) +
  geom_point() +
  xlim(c(0,8)) +
  ylim(c(0,4.5))

Created on 2020-06-12 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 6 月 12 日创建

While the question is not very clear, PoGibas seems to think that this is what the OP wanted.虽然问题不是很清楚,但 PoGibas 似乎认为这就是 OP 想要的。

library(tidyverse)

# default
iris %>% 
  ggplot(aes(Sepal.Length-6.2, Sepal.Width-3.2, col = Species)) +
  geom_point() +
  xlim(c(-2.5,2.5)) +
  ylim(c(-1.5,1.5)) +
  geom_hline(yintercept = 0)  +
  geom_vline(xintercept = 0)

Created on 2020-06-12 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 6 月 12 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM