在下图中,这是一个简单的散点图 + theme_apa(),我希望两个轴都通过 0。

我尝试了类似问题的答案中提出的一些解决方案,但没有一个奏效。
重现情节的 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))
Run Code Online (Sandbox Code Playgroud)
papaja 包可以通过以下方式安装
devtools::install_github("crsh/papaja")
Run Code Online (Sandbox Code Playgroud)
您要求的内容在 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()
Run Code Online (Sandbox Code Playgroud)
这尽可能接近使用 ggplot2 回答问题。
使用包 'ggpmisc' 我们可以稍微简化代码。
library(ggpmisc)
iris %>%
ggplot(aes(Sepal.Length - 5, Sepal.Width - 2, col = Species)) +
geom_quadrant_lines(linetype = "solid") +
geom_point() +
theme_minimal()
Run Code Online (Sandbox Code Playgroud)
此代码生成与上图完全相同的图。
如果您希望始终将原点居中,即无论数据范围如何,图形中的对称正负限制,那么包 'ggpmisc' 提供了一个简单的函数解决方案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()
Run Code Online (Sandbox Code Playgroud)
通过将+ theme(panel.grid = element_blank()) after 添加theme_minimal()到三个示例中的任何一个,可以从绘图区域中删除网格。
只为函数加载 'ggpmisc'symmetric_limits()是多余的,所以我在这里展示它的定义,它非常简单:
symmetric_limits <- function (x)
{
max <- max(abs(x))
c(-max, max)
}
Run Code Online (Sandbox Code Playgroud)