我试图在绘图上绘制p值,并且在某些情况下p值接近0或接近1(在下面的示例中,我仅将其编码为0或1进行了说明)目的)。
鉴于它们位于图的边缘,仅显示了geom_point的一半,这是不希望的。理想情况下,形状的另一半将显示在边界处,边缘在1和0处。我知道一个潜在的解决方案是更改/删除breaks()选项,但是我对它可能如何离开保持警惕空间,还有左侧(或右侧)的边界,该边界不会轻易向人们传达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处放置线条。
谢谢!
答案 0 :(得分: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创建