r - ggplot : shift position of y and x axis

标签 r ggplot2

我想将 y 轴和 x 轴的位置更改为零,尽管我有负值。我在 Google 小组中读到,当时 ggplot 是不可能的。此功能从那以后开始实现了吗?

例如,当我有这个数据时

library(ggplot2)

df1 <- data.frame(
  S = rep(c(10, 5, 3.3, 2.5, 1.66, 0.62), 2),
  v = c(c(0.202, 0.079, 0.0597, 0.0565, 0.0365, 0.0318), 
        c(0.25, 0.14, 0.07, 0.06, 0.04, 0.03)),
  inhibitor = rep(c("nein", "ja"), each = 6)
)

我想根据我手动添加的hlinevline改变坐标轴的位置。

ggplot(df1, aes(y = v, x = S, shape = inhibitor)) + 
  geom_point() +
  xlim(c(-2, 10)) + 
  geom_smooth(aes(linetype = inhibitor), 
              method = "lm", se = FALSE, 
              fullrange = TRUE, color = "black") +
  geom_vline(xintercept = 0) +
  geom_hline(yintercept = 0)

enter image description here

最佳答案

您是在谈论删除轴和轴标签之间的空间吗?

ggplot2 的比例函数中的 expand 参数设置绘图限制和轴之间的填充。

ggplot(df1, aes(y = v, x = S, shape = inhibitor)) + 
    geom_point() +
    xlim(c(-2, 10)) + 
    geom_smooth(aes(linetype = inhibitor), 
                method = "lm", se = FALSE, 
                fullrange = TRUE, color = "black") +
    geom_vline(xintercept = 0) +
    geom_hline(yintercept = 0) + 
    scale_x_continuous(expand=c(0,0)) +
    scale_y_continuous(expand=c(0,0))

enter image description here

关于r - ggplot : shift position of y and x axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55531308/

相关文章:

r - 使用Dockerfile进行Dockerizing Shiny-app

r - 是否可以使用 geom_text 将文本放在每个构面面板下方并将其与刻度线对齐?

r - ggplot2:每层单独的图例

r - ggplot2中的迷你图

r - 调试lapply/sapply调用

r - 尝试查找几何分布的 MLE 时出错

r - 在 mutate 部分返回几个向量的函数

r - 如何使用 R 中的 smatr 包绘制主轴拟合的置信区间?

r - 你如何具体排序 ggplot2 x 轴而不是字母顺序?

r - 带有 ggplot 的错误栏 - 出现错误