0

I am attempting to plot p-values on a plot, and there are some instances where the p-value is either close to 0 or close to 1 (in the example below, I just coded these as 0 or 1 for illustrative purposes).

Given that they are in the edges of the plot, only half of the geom_point shows up, which is undesirable. Ideally, the other half of the shapes would show over the border, where the edges are at 1 and 0. I understand that one potential solution would be to change/remove the breaks() option, but I am wary of how it might leave space but also the border to the left (or right) that would not easily convey to people that the p-value is either between 0 and 1.

Here is the code:

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

To other words, I am wary of pulling out the borders on either side, because I think this could suggest to readers that the bounds are not 0 or 1. If either a) geom_points could spill over borders, or b) borders could be pulled out but then the borders on the left and right are made white, and I could manually put lines at 0 and 1, that would be desirable.

Thanks!

enter image description here

user20650
  • 24,654
  • 5
  • 56
  • 91
rpickmans
  • 103
  • 8
  • 1
    you could use `coord_flip(ylim = c(0,1), clip="off")` but it looks kind off naff – user20650 Oct 27 '18 at 19:16
  • 1
    [Avoid clipping of points along axis in ggplot](https://stackoverflow.com/questions/9690648/point-clipped-on-x-axis-in-ggplot) – Henrik Oct 27 '18 at 19:17
  • @user20650 I am getting a return response of "Error in coord_flip(ylim = c(0, 1), clip = "off") : unused argument (clip = "off")", do you know why this might be? – rpickmans Oct 27 '18 at 19:43
  • 2
    Update ggplot. Please read the link I posted. – Henrik Oct 27 '18 at 19:46

1 Answers1

1

You can add clip = "off" to your coordinate specification.

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

Created on 2018-10-27 by the reprex package (v0.2.1.9000)

Elio Campitelli
  • 1,408
  • 1
  • 10
  • 20