Quantcast

r-plot

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

r-plot

durden10
Dear R-community

I have a grueling problem which appears to be impossible to solve:
I want to make a simple plot, here is my code: http://gist.github.com/118550
Unfortunately, the annotation of both the x- and y-axis are not correct, as you can see in the following picture:
I am not an expert of R, so maybe someone can point me to the solution of this problem, i.e. both of the axes should start and end at the min / max values of the two vectors.

Thanks in advance!!

Best,
Durden
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: r-plot

Jim Lemon
durden10 wrote:

> Dear R-community
>
> I have a grueling problem which appears to be impossible to solve:
> I want to make a simple plot, here is my code: http://gist.github.com/118550
> Unfortunately, the annotation of both the x- and y-axis are not correct, as
> you can see in the following picture:
> http://www.nabble.com/file/p23739356/plot.png 
> I am not an expert of R, so maybe someone can point me to the solution of
> this problem, i.e. both of the axes should start and end at the min / max
> values of the two vectors.
>
>  
Hi Durden,
This example seems to work for me. Is it just the X and Y axis labels
that you want?

data_corr<-data.frame(
 Win=c(-0.08,-0.07,-0.01,-0.01,0.03,0.08,0.1,0.13,
  0.18,0.19,0.195,0.2,0.28,0.3,0.4),
 Calgary=c(11,7,5,4,3,8,6,7,3,2,1,8,0,1,3)
)
par(tcl=0.35,xaxs="r") # Switch tick marks to insides of axes
plot(data_corr, type = "p", xlab="VS signal change",
 ylab="Depression scale",axes=FALSE, col = "blue", lwd = 2)
#y-axis
axis(2, tcl=0.35,at=0:11)
#x-axis
test2<-seq(0,0.4,by=0.1)
axis(1, tcl=0.35,at=test2)
box()
abline(lm(data_corr[,2]~data_corr[,1]))

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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: r-plot

Gavin Simpson
In reply to this post by durden10
On Wed, 2009-05-27 at 02:52 -0700, durden10 wrote:

> Dear R-community
>
> I have a grueling problem which appears to be impossible to solve:
> I want to make a simple plot, here is my code: http://gist.github.com/118550
> Unfortunately, the annotation of both the x- and y-axis are not correct, as
> you can see in the following picture:
> http://www.nabble.com/file/p23739356/plot.png 
> I am not an expert of R, so maybe someone can point me to the solution of
> this problem, i.e. both of the axes should start and end at the min / max
> values of the two vectors.
But you asked it to do that, explicitly:

par(tcl=0.35,xaxs="r")

xaxs = "r" is the default and if you read ?par it will tell you that
this extends the range of the plot by 4%. Pretty labels are then found
within this range.

Try xaxs = "i" and yaxs = "i" in your call instead.

Then you do this:

axis(2, tcl=0.35,at=0:11)

But 11 is outside the range of the plotted data (+4%) so this tick isn't
drawn. The plot() call sets up the region - a subsequent call to axis()
won't change the axis limits. If you want it to extend up to 11, then
add:

ylim = c(0,11) in your call to plot.

Note also that either the tcl in the first par() call or the ones in the
two axis calls is redundant. Use one or the other.

Here is a simplified example:

set.seed(123)
y <- 0:11 + rnorm(12)
x <- runif(12)
## if you want the 4% padding, then xaxs = "r" etc
op <- par(xaxs = "i", yaxs = "i", tcl = 0.35)
plot(x, y, ylim = c(0,11), axes = FALSE)
axis(2, at = 0:11)
axis(1)
box()
par(op)

Finally, as you have your data in a DF, you could make use of this
instead of relying on getting the ordering correct, and also simplify
your lm call:

plot(Calgary ~ Win, data = data_corr, ....)

and

abline(lm(Calgary ~ Win, data = data_corr, ....))

would be a better way to make use of the formula interface, and be
explicit in the plot about which variable is on the x and which is on
the y axis.

HTH

G

>
> Thanks in advance!!
>
> Best,
> Durden
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%


______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.

signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: r-plot

Richard Cotton
In reply to this post by durden10
> I want to make a simple plot, here is my code:
http://gist.github.com/118550
> Unfortunately, the annotation of both the x- and y-axis are not correct,
as
> you can see in the following picture:
> http://www.nabble.com/file/p23739356/plot.png 
> I am not an expert of R, so maybe someone can point me to the solution
of
> this problem, i.e. both of the axes should start and end at the min /
max
> values of the two vectors.

>From the help page on par:

'xaxs' The style of axis interval calculation to be used for the
          x-axis.  Possible values are '"r"', '"i"', '"e"', '"s"',
          '"d"'.  The styles are generally controlled by the range of
          data or 'xlim', if given. Style '"r"' (regular) first extends
          the data range by 4 percent at each end and then finds an
          axis with pretty labels that fits within the extended range.
          Style '"i"' (internal) just finds an axis with pretty labels
          that fits within the original data range.

You've explicitly set xaxs="r", when you really want xaxs="i".  You can
also explicitly set the axis limits using xlim/ylim parameters in the call
to plot.

Compare these examples:
#Ex 1
plot(1:10)      #implicitly uses par(xaxs="r", yaxs="r") unless you've
changed something

#Ex 2
oldpar <- par(xaxs="i", yaxs="i")
plot(1:10)
par(oldpar)

#Ex 3
plot(1:10, xlim=c(-5, 15), ylim=c(-100, 100))

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

r-plot 2nd attempt

durden10
In reply to this post by durden10
First of all, thanks a lot for your quick & helpful comments!

I have come down to this:

Win<- c(-0.005276404,  0.081894394, -0.073461539,  0.184371967,  0.133189670, -0.006239016,      -0.063616699,  0.196754234,  0.402148743,  0.104408425,
          0.036910154,  0.195227863,  0.212743723,  0.280889666,  0.300277802)
Calgary<- c(5,  8, 11,  3,  7,  4,  7,  1,  3,  6,  3,  2,  8,  0,  1)

data_corr <- data.frame(Win,Calgary)
plot(data_corr, type = "p", axes=FALSE,  col = "blue", lwd = 2)

#y-axis
axis(2, tcl=0.35,seq(1,11,by=2))

#x-axis
axis(1, tcl=0.35,seq(-0.1,0.5,by=0.1))
box()

abline(lm(data_corr[,2]~data_corr[,1]))

It works for the y-axis, but unfortunately, the x-axis is still not working: It starts at 0 and end at 0.4, but it should start at -0.1, as mentioned in the code (cf picture)  
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: r-plot 2nd attempt

Gavin Simpson
On Wed, 2009-05-27 at 06:06 -0700, durden10 wrote:

> First of all, thanks a lot for your quick & helpful comments!
>
> I have come down to this:
>
> Win<- c(-0.005276404,  0.081894394, -0.073461539,  0.184371967,
> 0.133189670, -0.006239016,      -0.063616699,  0.196754234,  0.402148743,
> 0.104408425,
>  0.036910154,  0.195227863,  0.212743723,  0.280889666,  0.300277802)
> Calgary<- c(5,  8, 11,  3,  7,  4,  7,  1,  3,  6,  3,  2,  8,  0,  1)
>
> data_corr <- data.frame(Win,Calgary)
> plot(data_corr, type = "p", axes=FALSE,  col = "blue", lwd = 2)
>
> #y-axis
> axis(2, tcl=0.35,seq(1,11,by=2))
>
> #x-axis
> axis(1, tcl=0.35,seq(-0.1,0.5,by=0.1))
> box()
>
> abline(lm(data_corr[,2]~data_corr[,1]))
>
> It works for the y-axis, but unfortunately, the x-axis is still not working:
> It starts at 0 and end at 0.4, but it should start at -0.1

Why? Your data extend to ~-0.07.

> , as mentioned in
> the code (cf picture) :confused:
> http://www.nabble.com/file/p23742121/Rplots_2.png 

You didn't adjust the x-axis limits to tell R that you wanted -0.1 to be
include on the lower end of the axis. R isn't clairvoyant you know!

I've tidied your script and altered it to extend the x-axis a bit so
that -0.1 is included:

Win <- c(-0.005276404,  0.081894394, -0.073461539, 0.184371967,
         0.133189670, -0.006239016, -0.063616699, 0.196754234,  
         0.402148743, 0.104408425, 0.036910154, 0.195227863,
         0.212743723, 0.280889666, 0.300277802)
Calgary<- c(5,  8, 11,  3,  7,  4,  7,  1,  3,  6,  3,  2,  8,  0,  1)

data_corr <- data.frame(Win, Calgary)
plot(Calgary ~ Win, data = data_corr, type = "p", axes = FALSE,  
     col = "blue", lwd = 2, xlim = c(-0.1,0.5))
##                          ^^^^^^^^^^^^^^^^^^
axis(2, tcl = 0.35, seq(1, 11, by=2))
axis(1, tcl = 0.35, seq(-0.1, 0.5, by=0.1))
box()
abline(lm(Calgary ~ Win, data = data_corr))

HTH

G
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

r-plot problem resolved

durden10
In reply to this post by durden10
Thanks a lot for all the helpful comments! It finally works
(I settled with the code of Gavin)

Best,
Durden
Loading...