Plot problems: xlim

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

Plot problems: xlim

Ronnie Babigumira
Hi,
Still fresh in R, tried to figure this out, now on my second day running with no luck (and a pile of hair on my desk) so
I have thrown in the towel and would like to ask for some help.

Here is what I am trying to do. I am trying to plot a distribution, I have 99 points, bound in the range

xlim.min: -0.0173
xlim.max: 0.02103

However, I have a value outside this range (0.2454959) which I would like to add to the plot as a line and to do this I
use abline(v=0.2454959)

This is what I write

 >xlim = c(-0.02, 0.3)
 >denz <- density(morp)
 >plot.density(denz, xlim = xlim, ylim = c(0,70))
 >hist(morp, freq=F, add= T)
 >abline(v=0.2454959)

Without any options, plot.density spreads out nicely, however, naturally, the line I want to add is not plotted since it
is well outside the range automatically determined by plot.density hence the need to add xlim however this produces
something I dont find aesthetically appealing. The plot is squeezed out into a very lean "bell" shape.

So (finally) my question, how can i widen the spread of my plot and yet also be able to add my xline.

Many thanks

Ronnie

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems: xlim

Jacques VESLOT
Please give an example of your data.

set.seed(231)
morp <- rnorm(20)
range(morp)
[1] -2.311664  1.650254

You can plot 2 histograms, one of them with the extreme value:

par(mfrow=c(2,1))
hist(morp, breaks=10, freq=F)
lines(density(morp))
par(mfrow=c(1,2))
hist(morp, breaks=10, freq=F)
lines(density(morp))
hist(morp, breaks=seq(min(morp), max(morp), length=10), xlim=c(-3, 13),
freq=F)
lines(density(morp))
abline(v=7.5, lty=3)


Ronnie Babigumira a écrit :

>Hi,
>Still fresh in R, tried to figure this out, now on my second day running with no luck (and a pile of hair on my desk) so
>I have thrown in the towel and would like to ask for some help.
>
>Here is what I am trying to do. I am trying to plot a distribution, I have 99 points, bound in the range
>
>xlim.min: -0.0173
>xlim.max: 0.02103
>
>However, I have a value outside this range (0.2454959) which I would like to add to the plot as a line and to do this I
>use abline(v=0.2454959)
>
>This is what I write
>
> >xlim = c(-0.02, 0.3)
> >denz <- density(morp)
> >plot.density(denz, xlim = xlim, ylim = c(0,70))
> >hist(morp, freq=F, add= T)
> >abline(v=0.2454959)
>
>Without any options, plot.density spreads out nicely, however, naturally, the line I want to add is not plotted since it
>is well outside the range automatically determined by plot.density hence the need to add xlim however this produces
>something I dont find aesthetically appealing. The plot is squeezed out into a very lean "bell" shape.
>
>So (finally) my question, how can i widen the spread of my plot and yet also be able to add my xline.
>
>Many thanks
>
>Ronnie
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>  
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems: xlim

Ben Bolker
In reply to this post by Ronnie Babigumira
Ronnie Babigumira <rb.glists <at> gmail.com> writes:

   It sounds like you might want to break your axis.
plotrix provides a function to draw the axis break,
but you have to mess around with the data scaling
and axis labels yourself.  See  RSiteSearch("axis
break"); most of these discussions
are about breaking y axes but the same techniques
apply to the x axis.

  good luck,
    Ben Bolker

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems: xlim

Ronnie Babigumira
Many thanks Jacques Veslot and Ben Bolker, Im closer to my goal thanks to Jacques's code and Ben I will follow the leads.

Happy Holidays

Ronnie

Ronnie
Ben Bolker wrote:

> Ronnie Babigumira <rb.glists <at> gmail.com> writes:
>
>    It sounds like you might want to break your axis.
> plotrix provides a function to draw the axis break,
> but you have to mess around with the data scaling
> and axis labels yourself.  See  RSiteSearch("axis
> break"); most of these discussions
> are about breaking y axes but the same techniques
> apply to the x axis.
>
>   good luck,
>     Ben Bolker
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems: xlim

Jim Lemon-2
In reply to this post by Ronnie Babigumira
Ronnie Babigumira wrote:

> Hi,
> Still fresh in R, tried to figure this out, now on my second day running with no luck (and a pile of hair on my desk) so
> I have thrown in the towel and would like to ask for some help.
>
> Here is what I am trying to do. I am trying to plot a distribution, I have 99 points, bound in the range
>
> xlim.min: -0.0173
> xlim.max: 0.02103
>
> However, I have a value outside this range (0.2454959) which I would like to add to the plot as a line and to do this I
> use abline(v=0.2454959)
>
> This is what I write
>
>  >xlim = c(-0.02, 0.3)
>  >denz <- density(morp)
>  >plot.density(denz, xlim = xlim, ylim = c(0,70))
>  >hist(morp, freq=F, add= T)
>  >abline(v=0.2454959)
>
> Without any options, plot.density spreads out nicely, however, naturally, the line I want to add is not plotted since it
> is well outside the range automatically determined by plot.density hence the need to add xlim however this produces
> something I dont find aesthetically appealing. The plot is squeezed out into a very lean "bell" shape.
>
> So (finally) my question, how can i widen the spread of my plot and yet also be able to add my xline.
>
Hi Ronnie,

For only one line, it is probably easiest to stick in an axis break and
label the line on the x axis. Notice that the position of the line is
arbitrarily set to be far enough beyond the end of the density curve to
allow room for the axis break.

testdata<-rnorm(50,sd=0.01)
denz<-density(testdata)
plot(denz,xlim=c(-0.02,0.04),axes=FALSE)
box()
axis(1,at=c(-0.02,0,0.02,0.039),labels=c(-0.02,0,0.02,0.2454959))
abline(v=0.039)
axis.break(1,breakpos=0.037)

This is probably a common enough problem for inclusion in the axis.break
example.

Jim

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems: xlim

Haynes, Maurice (NIH/NICHD) [E]
In reply to this post by Ronnie Babigumira
Hi,

I believe Jim Lemon's solution requires his plotrix package.

Maurice Haynes
National Institutes of Health
Child and Family Research Section


-----Original Message-----
From: Jim Lemon [mailto:[hidden email]]
Sent: Friday, December 23, 2005 10:12 PM
To: Ronnie Babigumira
Cc: R Help
Subject: Re: [R] Plot problems: xlim

Ronnie Babigumira wrote:
> Hi,
> Still fresh in R, tried to figure this out, now on my second day
running with no luck (and a pile of hair on my desk) so
> I have thrown in the towel and would like to ask for some help.
>
> Here is what I am trying to do. I am trying to plot a distribution, I
have 99 points, bound in the range
>
> xlim.min: -0.0173
> xlim.max: 0.02103
>
> However, I have a value outside this range (0.2454959) which I would
like to add to the plot as a line and to do this I

> use abline(v=0.2454959)
>
> This is what I write
>
>  >xlim = c(-0.02, 0.3)
>  >denz <- density(morp)
>  >plot.density(denz, xlim = xlim, ylim = c(0,70))
>  >hist(morp, freq=F, add= T)
>  >abline(v=0.2454959)
>
> Without any options, plot.density spreads out nicely, however,
naturally, the line I want to add is not plotted since it
> is well outside the range automatically determined by plot.density
hence the need to add xlim however this produces
> something I dont find aesthetically appealing. The plot is squeezed
out into a very lean "bell" shape.
>
> So (finally) my question, how can i widen the spread of my plot and
yet also be able to add my xline.
>
Hi Ronnie,

For only one line, it is probably easiest to stick in an axis break and
label the line on the x axis. Notice that the position of the line is
arbitrarily set to be far enough beyond the end of the density curve to
allow room for the axis break.

testdata<-rnorm(50,sd=0.01)
denz<-density(testdata)
plot(denz,xlim=c(-0.02,0.04),axes=FALSE)
box()
axis(1,at=c(-0.02,0,0.02,0.039),labels=c(-0.02,0,0.02,0.2454959))
abline(v=0.039)
axis.break(1,breakpos=0.037)

This is probably a common enough problem for inclusion in the axis.break

example.

Jim

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems: xlim

hadley wickham
In reply to this post by Ronnie Babigumira
> Without any options, plot.density spreads out nicely, however, naturally, the line I want to add is not plotted since it
> is well outside the range automatically determined by plot.density hence the need to add xlim however this produces
> something I dont find aesthetically appealing. The plot is squeezed out into a very lean "bell" shape.


Using a broken axis is not a good solution to this problem (and there
are very few times that using a broken axis is a good idea)

It sounds like you are trying to compare a reference value to a
distribution.  To do this visually they both need to be on the same
scale, so that you can see the distance between the reference value
and the distribution.

Although it may not be aesthetically pleasing, it is true to the data.

Hadley

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems: xlim

Ronnie Babigumira
In reply to this post by Haynes, Maurice (NIH/NICHD) [E]
Maurice, Indeed it does (tried to run it and got an error..however a quick search led me to it).

Jim, many thanks for your code...I am now trying out all the ideas and will get back with what works best for me

Ronnie
Haynes, Maurice (NIH/NICHD) [E] wrote:

> Hi,
>
> I believe Jim Lemon's solution requires his plotrix package.
>
> Maurice Haynes
> National Institutes of Health
> Child and Family Research Section
>
>
> -----Original Message-----
> From: Jim Lemon [mailto:[hidden email]]
> Sent: Friday, December 23, 2005 10:12 PM
> To: Ronnie Babigumira
> Cc: R Help
> Subject: Re: [R] Plot problems: xlim
>
> Ronnie Babigumira wrote:
>> Hi,
>> Still fresh in R, tried to figure this out, now on my second day
> running with no luck (and a pile of hair on my desk) so
>> I have thrown in the towel and would like to ask for some help.
>>
>> Here is what I am trying to do. I am trying to plot a distribution, I
> have 99 points, bound in the range
>> xlim.min: -0.0173
>> xlim.max: 0.02103
>>
>> However, I have a value outside this range (0.2454959) which I would
> like to add to the plot as a line and to do this I
>> use abline(v=0.2454959)
>>
>> This is what I write
>>
>>  >xlim = c(-0.02, 0.3)
>>  >denz <- density(morp)
>>  >plot.density(denz, xlim = xlim, ylim = c(0,70))
>>  >hist(morp, freq=F, add= T)
>>  >abline(v=0.2454959)
>>
>> Without any options, plot.density spreads out nicely, however,
> naturally, the line I want to add is not plotted since it
>> is well outside the range automatically determined by plot.density
> hence the need to add xlim however this produces
>> something I dont find aesthetically appealing. The plot is squeezed
> out into a very lean "bell" shape.
>> So (finally) my question, how can i widen the spread of my plot and
> yet also be able to add my xline.
> Hi Ronnie,
>
> For only one line, it is probably easiest to stick in an axis break and
> label the line on the x axis. Notice that the position of the line is
> arbitrarily set to be far enough beyond the end of the density curve to
> allow room for the axis break.
>
> testdata<-rnorm(50,sd=0.01)
> denz<-density(testdata)
> plot(denz,xlim=c(-0.02,0.04),axes=FALSE)
> box()
> axis(1,at=c(-0.02,0,0.02,0.039),labels=c(-0.02,0,0.02,0.2454959))
> abline(v=0.039)
> axis.break(1,breakpos=0.037)
>
> This is probably a common enough problem for inclusion in the axis.break
>
> example.
>
> Jim
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems: xlim

Ronnie Babigumira
In reply to this post by hadley wickham
Hadley
Your point is valid and well taken. However, the break still shows how far off the value is from the distribution (I
intend to add a note to draw a readers attention to this). Anyhow, like I said, I will try the two ideas shared on the
list and make a note of what works best for me.

Many thanks

Ronnie
hadley wickham wrote:

>> Without any options, plot.density spreads out nicely, however, naturally, the line I want to add is not plotted since it
>> is well outside the range automatically determined by plot.density hence the need to add xlim however this produces
>> something I dont find aesthetically appealing. The plot is squeezed out into a very lean "bell" shape.
>
>
> Using a broken axis is not a good solution to this problem (and there
> are very few times that using a broken axis is a good idea)
>
> It sounds like you are trying to compare a reference value to a
> distribution.  To do this visually they both need to be on the same
> scale, so that you can see the distance between the reference value
> and the distribution.
>
> Although it may not be aesthetically pleasing, it is true to the data.
>
> Hadley
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Plot problems: xlim

P Ehlers
While I appreciate the availability of axis.break, I agree with Hadley
in this case. I would provide two plots, with and without the special
point. Or just the density plot and some numbers. Broken axes require
interpretation which is often easier to do using numbers, e.g. the
mean or range (exclusive of x.special) and x.special. Sometimes
simple numbers really do provide a better 'picture'.

Peter Ehlers

Ronnie Babigumira wrote:

> Hadley
> Your point is valid and well taken. However, the break still shows how far off the value is from the distribution (I
> intend to add a note to draw a readers attention to this). Anyhow, like I said, I will try the two ideas shared on the
> list and make a note of what works best for me.
>
> Many thanks
>
> Ronnie
> hadley wickham wrote:
>
>>>Without any options, plot.density spreads out nicely, however, naturally, the line I want to add is not plotted since it
>>>is well outside the range automatically determined by plot.density hence the need to add xlim however this produces
>>>something I dont find aesthetically appealing. The plot is squeezed out into a very lean "bell" shape.
>>
>>
>>Using a broken axis is not a good solution to this problem (and there
>>are very few times that using a broken axis is a good idea)
>>
>>It sounds like you are trying to compare a reference value to a
>>distribution.  To do this visually they both need to be on the same
>>scale, so that you can see the distance between the reference value
>>and the distribution.
>>
>>Although it may not be aesthetically pleasing, it is true to the data.
>>
>>Hadley
>>
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html