How to avoid ggplot clipping the x axis [damn, replaces previous Email]

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to avoid ggplot clipping the x axis [damn, replaces previous Email]

Chris Evans
[I must try to remember that swapping between Rstudio and my Emailer is a recipe for hitting ctrl-enter and posting prematurely: sorry!]

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))
# x labels on plot are 2, 4 & 6

ggplot(dat = tibDat,
aes(x = x, y = y)) +
geom_point() +
expand_limits(x = c(1,6))
# same

ggplot(dat = tibDat,
aes(x = x, y = y)) +
geom_point() +
scale_x_continuous(breaks = 1:6, labels = 1:6)
# x labelled at 2:5 (why?!)

ggplot(dat = tibDat,
aes(x = x, y = y)) +
geom_point() +
xlim(c(1,6)) +
coord_cartesian(clip = "off")
# back to 2, 4, 6

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

### OK, getting desperately hopeful
ggplot(dat = tibDat,
aes(x = x, y = y)) +
geom_point() +
xlim(c(0,6))
# x axis has 0, 2, 4 and 6 (surely inconsistent behaviour?)

ggplot(dat = tibDat,
aes(x = x, y = y)) +
geom_point() +
xlim(c(0.1,6))
# x axis has 0, 2, 4 and 6

Can anyone see the simple answer that is eluding me?!

TIA,

Chris


--
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 <[hidden email]> Visiting Professor, University of Sheffield <[hidden email]>
I do some consultation work for the University of Roehampton <[hidden email]> and other places
but <[hidden email]> 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.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to avoid ggplot clipping the x axis [damn, replaces previous Email]

Rui Barradas
Hello,

You are almost there.

With scale_x_continuous use argument limits.


ggplot(dat = tibDat,
        aes(x = x, y = y)) +
   geom_point() +
   scale_x_continuous(breaks = 1:6, labels = 1:6,
                      limits = c(1, 6))


And with xlim it's not a vector c(1, 6), it's each element on its own:

xlim(1, 6)


Or

lims(x = c(1, 6))


But with these two, the labels are not like you want them, they are 2 by 2.

Hope this helps,

Rui Barradas

Às 10:34 de 10/01/21, Chris Evans escreveu:

> [I must try to remember that swapping between Rstudio and my Emailer is a recipe for hitting ctrl-enter and posting prematurely: sorry!]
>
> 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))
> # x labels on plot are 2, 4 & 6
>
> ggplot(dat = tibDat,
> aes(x = x, y = y)) +
> geom_point() +
> expand_limits(x = c(1,6))
> # same
>
> ggplot(dat = tibDat,
> aes(x = x, y = y)) +
> geom_point() +
> scale_x_continuous(breaks = 1:6, labels = 1:6)
> # x labelled at 2:5 (why?!)
>
> ggplot(dat = tibDat,
> aes(x = x, y = y)) +
> geom_point() +
> xlim(c(1,6)) +
> coord_cartesian(clip = "off")
> # back to 2, 4, 6
>
> ggplot(dat = tibDat,
> aes(x = x, y = y)) +
> geom_point() +
> xlim(c(1,6)) +
> coord_cartesian(expand = TRUE)
> # same
>
> ### OK, getting desperately hopeful
> ggplot(dat = tibDat,
> aes(x = x, y = y)) +
> geom_point() +
> xlim(c(0,6))
> # x axis has 0, 2, 4 and 6 (surely inconsistent behaviour?)
>
> ggplot(dat = tibDat,
> aes(x = x, y = y)) +
> geom_point() +
> xlim(c(0.1,6))
> # x axis has 0, 2, 4 and 6
>
> Can anyone see the simple answer that is eluding me?!
>
> TIA,
>
> Chris
>
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to avoid ggplot clipping the x axis [damn, replaces previous Email]

Chris Evans
Perfect.  Can't believe I failed to find that!  Thanks, as ever, Rui

----- Original Message -----
> From: "Rui Barradas" <[hidden email]>
> To: "Chris Evans" <[hidden email]>, [hidden email]
> Sent: Sunday, 10 January, 2021 18:10:16
> Subject: Re: [R] How to avoid ggplot clipping the x axis [damn, replaces previous Email]

> Hello,
>
> You are almost there.
>
> With scale_x_continuous use argument limits.
>
>
> ggplot(dat = tibDat,
>        aes(x = x, y = y)) +
>   geom_point() +
>   scale_x_continuous(breaks = 1:6, labels = 1:6,
>                      limits = c(1, 6))
>
>
> And with xlim it's not a vector c(1, 6), it's each element on its own:
>
> xlim(1, 6)
>
>
> Or
>
> lims(x = c(1, 6))
>
>
> But with these two, the labels are not like you want them, they are 2 by 2.
>
> Hope this helps,
>
> Rui Barradas
>
> Às 10:34 de 10/01/21, Chris Evans escreveu:
>> [I must try to remember that swapping between Rstudio and my Emailer is a recipe
>> for hitting ctrl-enter and posting prematurely: sorry!]
>>
>> 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))
>> # x labels on plot are 2, 4 & 6
>>
>> ggplot(dat = tibDat,
>> aes(x = x, y = y)) +
>> geom_point() +
>> expand_limits(x = c(1,6))
>> # same
>>
>> ggplot(dat = tibDat,
>> aes(x = x, y = y)) +
>> geom_point() +
>> scale_x_continuous(breaks = 1:6, labels = 1:6)
>> # x labelled at 2:5 (why?!)
>>
>> ggplot(dat = tibDat,
>> aes(x = x, y = y)) +
>> geom_point() +
>> xlim(c(1,6)) +
>> coord_cartesian(clip = "off")
>> # back to 2, 4, 6
>>
>> ggplot(dat = tibDat,
>> aes(x = x, y = y)) +
>> geom_point() +
>> xlim(c(1,6)) +
>> coord_cartesian(expand = TRUE)
>> # same
>>
>> ### OK, getting desperately hopeful
>> ggplot(dat = tibDat,
>> aes(x = x, y = y)) +
>> geom_point() +
>> xlim(c(0,6))
>> # x axis has 0, 2, 4 and 6 (surely inconsistent behaviour?)
>>
>> ggplot(dat = tibDat,
>> aes(x = x, y = y)) +
>> geom_point() +
>> xlim(c(0.1,6))
>> # x axis has 0, 2, 4 and 6
>>
>> Can anyone see the simple answer that is eluding me?!
>>
>> TIA,
>>
>> Chris
>>

--
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 <[hidden email]> Visiting Professor, University of Sheffield <[hidden email]>
I do some consultation work for the University of Roehampton <[hidden email]> and other places
but <[hidden email]> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.