[R] How to avoid ggplot clipping the x axis

Chris Evans chr|@ho|d @end|ng |rom p@yctc@org
Sun Jan 10 11:26:25 CET 2021


I am sure I am doing something stupid but I can't see what. I am plotting data and want the x axis labels to reflect the theoretical range of the x variable which happens to be 1:6 but the observed values have range 2.6 to 5.9. 

I thought xlim(c(1,6)) should have got me that quite simply but it doesn't.  I think this is a reproducible example of my failures (!)

library(tidyverse)
library(ggplot2)
x <- seq(2,5.8,.2)
list(x = x,
     y = rnorm(length(x))) %>%
  as_tibble() -> tibDat

ggplot(dat = tibDat,
       aes(x = x, y = y)) +
  geom_point() +
  xlim(c(1,6))
  
ggplot(dat = tibDat,
       aes(x = x, y = y)) +
  geom_point() +
  scale_x_continuous(breaks = 1:6, labels = 1:6) 
  
ggplot(dat = tibDat,
       aes(x = x, y = y)) +
  geom_point() +
  xlim(c(1,6)) +
  coord_cartesian(clip = "off")

ggplot(dat = tibDat,
       aes(x = x, y = y)) +
  geom_point() +
  xlim(c(1,6)) +
  coord_cartesian(expand = TRUE)

Bizarrely (to me)

ggplot(dat = tibDat,
       aes(x = x, y = y)) +
  geom_point() +
  xlim(c(0,6))

-- 
Small contribution in our coronavirus rigours: 
https://www.coresystemtrust.org.uk/home/free-options-to-replace-paper-core-forms-during-the-coronavirus-pandemic/

Chris Evans <chris using psyctc.org> Visiting Professor, University of Sheffield <chris.evans using sheffield.ac.uk>
I do some consultation work for the University of Roehampton <chris.evans using roehampton.ac.uk> and other places
but <chris using psyctc.org> remains my main Email address.  I have a work web site at:
   https://www.psyctc.org/psyctc/
and a site I manage for CORE and CORE system trust at:
   http://www.coresystemtrust.org.uk/
I have "semigrated" to France, see: 
   https://www.psyctc.org/pelerinage2016/semigrating-to-france/ 
   https://www.psyctc.org/pelerinage2016/register-to-get-updates-from-pelerinage2016/

If you want an Emeeting, I am trying to keep them to Thursdays and my diary is at:
   https://www.psyctc.org/pelerinage2016/ceworkdiary/
Beware: French time, generally an hour ahead of UK.



More information about the R-help mailing list