繁体   English   中英

如何在 ggplot2 中使轴居中

[英]How To Center Axes in ggplot2

在下面的 plot 中,这是一个简单的散点 plot + theme_apa(),我希望这两个轴 go 到 0。 在此处输入图像描述

我尝试了类似问题的答案中提出的一些解决方案,但没有一个奏效。

重现 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))

木瓜package可以通过安装

devtools::install_github("crsh/papaja")

在 ggplot2 中无法实现您的要求,并且有充分的理由,如果您在绘图区域内包含轴和刻度标签,它们迟早会与表示数据的点或线重叠。 我使用@phiggins 和@Job Nmadu 的答案作为起点。 我更改了几何图形的顺序以确保“数据”绘制在轴的顶部。 我将主题更改为theme_minimal()以便不在绘图区域之外绘制轴。 我修改了用于数据的偏移量,以更好地演示代码的工作方式。

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()

这尽可能接近使用 ggplot2 回答问题。

在此处输入图像描述

使用 package 'ggpmisc' 我们可以稍微简化代码。

library(ggpmisc)

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

此代码生成与上图所示完全相同的 plot。

如果您希望始终使原点居中,即无论数据范围如何,图中的对称正负限制,然后 package 'ggpmisc' 提供了一个简单的解决方案 function symmetric_limits() 这就是通常绘制基因表达和类似双向反应的象限图的方式。

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()

在此处输入图像描述

可以通过在theme_minimal()之后向三个示例中的任何一个添加+ theme(panel.grid = element_blank())来从绘图区域中删除网格。

只为 function symmetric_limits()加载 'ggpmisc' 太过分了,所以我在这里展示它的定义,非常简单:

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

作为记录,以下内容也如上所述。

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

设置 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))

代表 package (v0.3.0) 于 2020 年 6 月 12 日创建

虽然问题不是很清楚,但 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)

代表 package (v0.3.0) 于 2020 年 6 月 12 日创建

暂无
暂无

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

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