【问题标题】:Have geom_point shapes spill over borders [duplicate]让 geom_point 形状溢出边界[重复]
【发布时间】:2019-04-01 04:46:13
【问题描述】:

我试图在绘图上绘制 p 值,并且在某些情况下 p 值接近 0 或接近 1(在下面的示例中,我只是将它们编码为 0 或 1 以进行说明目的)。

由于它们位于绘图的边缘,因此只有一半的 geom_point 出现,这是不可取的。理想情况下,另一半形状将显示在边界上,边缘位于 1 和 0 处。我知道一种可能的解决方案是更改/删除 break() 选项,但我担心它可能会如何离开空间,还有左边(或右边)的边界,不容易向人们传达 p 值介于 0 和 1 之间。

代码如下:

library(dplyr)
library(ggplot2)

df <- data_frame(outcome = c("Outcome 1","Outcome 2", "Outcome 3", "Outcome 1","Outcome 2", "Outcome 3", "Outcome 1","Outcome 2", "Outcome 3"),
      sample = c("Indiana", "Indiana", "Indiana", "Colorado", "Colorado", "Colorado", "Virginia", "Virginia", "Virginia"),
      pvals_open = c(0.0, 0.120, 1, NA, 0.192, 0.121, NA, 1, 0.30),
      pvals_closed = c(NA, NA, NA, 0.029, NA, NA, 0.00, NA, NA)
)

df2 <- df %>% 
  mutate(
  val = coalesce(pvals_open, pvals_closed),
  sig = if_else(val > 0.05, "> 0.05", "<= 0.05")
) %>% select(outcome, sample, val, sig)

ggplot(df2) +
  aes(x = outcome, y = val, group = sample, colour = sample, shape = sig) +
 geom_point(size = 2, position = position_dodge(0.8)) +
 geom_hline(yintercept = 0.05, linetype = "dotted") +
 coord_flip(ylim = c(0,1)) +
 theme(
   # legend.justification=c(0, 1),
   # legend.position = "none",
   # legend.title = element_blank(),
   # legend.background = element_rect(fill = NA),
   text = element_text(size=11),
   panel.background = element_rect(fill = NA),
   panel.border = element_rect(fill = NA, color = 'grey75'),
   axis.ticks = element_blank(),
   plot.title = element_text(size=14),
   panel.grid.major.x = element_blank(),
   panel.grid.minor.x = element_blank()) +
 scale_y_reverse(breaks=c(0.00,0.05,0.5,1), expand=c(0,0), labels=c("0","0.05", "0.5","1"))

换句话说,我对拉出两边的边界持谨慎态度,因为我认为这可能会向读者暗示边界不是 0 或 1。如果 a) geom_points 可能溢出边界,或者 b) 边界可以拉出,但左右边框变成白色,我可以手动将线条放在 0 和 1 处,这将是可取的。

谢谢!

【问题讨论】:

  • 你可以使用coord_flip(ylim = c(0,1), clip="off"),但它看起来有点不对劲
  • @user20650 我收到“coord_flip 错误(ylim = c(0, 1), clip = "off") 的返回响应:未使用的参数(clip = "off")”,执行你知道为什么会这样吗?
  • 更新ggplot。请阅读我发布的链接。

标签: r ggplot2


【解决方案1】:

您可以将clip = "off" 添加到您的坐标规范中。

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

df <- data_frame(outcome = c("Outcome 1","Outcome 2", "Outcome 3", "Outcome 1","Outcome 2", "Outcome 3", "Outcome 1","Outcome 2", "Outcome 3"),
                 sample = c("Indiana", "Indiana", "Indiana", "Colorado", "Colorado", "Colorado", "Virginia", "Virginia", "Virginia"),
                 pvals_open = c(0.0, 0.120, 1, NA, 0.192, 0.121, NA, 1, 0.30),
                 pvals_closed = c(NA, NA, NA, 0.029, NA, NA, 0.00, NA, NA)
)

df2 <- df %>% 
  mutate(
    val = coalesce(pvals_open, pvals_closed),
    sig = if_else(val > 0.05, "> 0.05", "<= 0.05")
  ) %>% select(outcome, sample, val, sig)

ggplot(df2) +
  aes(x = outcome, y = val, group = sample, colour = sample, shape = sig) +
  geom_point(size = 2, position = position_dodge(0.8)) +
  geom_hline(yintercept = 0.05, linetype = "dotted") +
  coord_flip(ylim = c(0,1), clip = "off") +   # clip = "off"
  theme(
    # legend.justification=c(0, 1),
    # legend.position = "none",
    # legend.title = element_blank(),
    # legend.background = element_rect(fill = NA),
    text = element_text(size=11),
    panel.background = element_rect(fill = NA),
    panel.border = element_rect(fill = NA, color = 'grey75'),
    axis.ticks = element_blank(),
    plot.title = element_text(size=14),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank()) +
  scale_y_reverse(breaks=c(0.00,0.05,0.5,1), expand=c(0,0), labels=c("0","0.05", "0.5","1"))

reprex package (v0.2.1.9000) 于 2018 年 10 月 27 日创建

【讨论】:

  • 我正在复制并粘贴你给我看的代码,我被告知“coord_flip(ylim = c(0, 1), clip = "off") 中的错误:未使用的参数 (clip = "关闭")"
  • Mh... 可能是您使用的是旧版本的 ggplot2。尝试更新它。我认为使用install.packages("ggplot2") 可以解决问题。
  • 是的,就是这样。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多