looking for alternative of 'if-else'

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

looking for alternative of 'if-else'

Arun.stat
There is "if-else" loop if I have to choose 1 item from a 2-item list.
However if I have a list of 4 items (let say) then how i can choose a single
item without employing 'if-else' loop? I mean in VBA I can use
"select-case", is there any equivalent in R as well?

Regards,

        [[alternative HTML version deleted]]

______________________________________________
[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
|

Re: looking for alternative of 'if-else'

Duncan Murdoch
On 06/07/2008 4:59 AM, Arun Kumar Saha wrote:
> There is "if-else" loop if I have to choose 1 item from a 2-item list.
> However if I have a list of 4 items (let say) then how i can choose a single
> item without employing 'if-else' loop? I mean in VBA I can use
> "select-case", is there any equivalent in R as well?

See ?switch.

Duncan Murdoch

______________________________________________
[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
|

Re: looking for alternative of 'if-else'

CTU-2
In reply to this post by Arun.stat
Why don't you try "switch" Let me assume that you want to calculate  
a=3 and b =5 by using one of "MEAN", "SUM","MIN", and "MAX functions.  
Then you could write your code: (If you want to calculate "MEAN")

> example<-function(fun=c("MEAN", "SUM", "MIN", "MAX")){
+   fun<-match.arg(fun);a=3;b=5
+   switch(  fun,
+            MEAN=(a+b)/2,
+            SUM=a+b,
+            MIN=min(a,b),
+            MAX=max(a,b))
+            }
>             example("MEAN")
[1] 4
>
Hope this is helpful,
Chunhao Tu





Quoting Arun Kumar Saha <[hidden email]>:

> There is "if-else" loop if I have to choose 1 item from a 2-item list.
> However if I have a list of 4 items (let say) then how i can choose a single
> item without employing 'if-else' loop? I mean in VBA I can use
> "select-case", is there any equivalent in R as well?
>
> Regards,
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.
>

______________________________________________
[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
|

"lwd" gives different plotted line widths for Vertical and horizontal Lines when lwd= 2?

Pedro Conte de Barros
In reply to this post by Duncan Murdoch
Dear All,

I have been trying to add lines to the axis grobs of plots produced
with ggplot2.

The code I have used is below. It works, although I do not think it
is a really elegant way of doing what I want....
However, I am now noticing that when plotted, the width of the lines
in the axis are not the same as the line widths I get for the ticks
(the ticks get thicker lines).  Also, when I build a function to do
the same conversion, I get different (thinner) line widths for the
x-axis I have been trying to find out why this is so, but I seem to
have met a dead-end.

#Test
library(ggplot2)
#Set the basic overall plot options
themePublish <-list(background.fill='white', grid.fill='white',
grid.colour='NA', grid.minor.colour='NA', axis.colour='black',
border.colour='NA')
ggtheme(themePublish)

#Generate the data
plotdata<-data.frame(x=1:10, y=runif(10))

#Build the ggplot object

ggPlotObject<-ggplot()
ggPlotObject<-ggPlotObject+layer(data=plotdata,
mapping=aes_string(x='x',y='y'),geom='point', stat='identity', size=4)
ggPlotObject<-ggPlotObject+ scale_y_continuous(limits=c(0,1), expand=c(0,0))

#################
#I define the following function
F.ggPlot.AddAxisLines<-function(ggPlotObject, col='black', lwd=2, lty='solid'){
###################################################################################################
# First, capture the ggPlot object as a gTree
ggPlotTree<-ggplot_plot(ggPlotObject)

#Get the parameters defined for the lines
LinePars <- gpar(col=col, lwd=lwd, lty=lty)

#Then, create the objects to add to the axes
YAxis.Line<-linesGrob(x=unit(c(1,1), 'npc'), y=unit(c(0,1), 'npc'),
vp='left_axis', name='YAxisLine', gp=LinePars)
XAxis.Line<-linesGrob(x=unit(c(0,1), 'npc'), y=unit(c(1,1), 'npc'),
vp='bottom_axis', name='XAxisLine', gp=LinePars)

#Add the grobs to this object
ggPlotTreeMod<-ggPlotTree
ggPlotTreeMod<-addGrob(ggPlotTreeMod, YAxis.Line, gPath('yaxis'), grep=TRUE)
ggPlotTreeMod<-addGrob(ggPlotTreeMod, XAxis.Line, gPath('xaxis'), grep=TRUE)

#Edit the properties of the ticks, to match those of the lines
ggPlotTreeMod<-editGrob(ggPlotTreeMod, gPath('ticks'), gp=LinePars,
grep=TRUE, global=TRUE)

#Output the new object

ggPlotTreeMod
}

# and then do
ggPlotTreeMod2 <- F.ggPlot.AddAxisLines(ggPlotObject)
grid.draw(ggPlotTreeMod2)

# I get different line thicknesses for the two axes

#However, if I do
ggPlotTreeMod3 <- F.ggPlot.AddAxisLines(ggPlotObject, lwd=3)
grid.draw(ggPlotTreeMod3)

# I get the same line thickness on both axes, but thicker ticks


I would really appreciate any pointer or hint.
Thanks,
Pedro

______________________________________________
[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
|

Re: "lwd" gives different plotted line widths for Vertical and horizontal Lines when lwd= 2?

hadley wickham
On Sun, Jul 6, 2008 at 6:33 AM, Pedro de Barros <[hidden email]> wrote:

> Dear All,
>
> I have been trying to add lines to the axis grobs of plots produced with
> ggplot2.
>
> The code I have used is below. It works, although I do not think it is a
> really elegant way of doing what I want....
> However, I am now noticing that when plotted, the width of the lines in the
> axis are not the same as the line widths I get for the ticks (the ticks get
> thicker lines).  Also, when I build a function to do the same conversion, I
> get different (thinner) line widths for the x-axis I have been trying to
> find out why this is so, but I seem to have met a dead-end.
>
> #Test
> library(ggplot2)
> #Set the basic overall plot options
> themePublish <-list(background.fill='white', grid.fill='white',
> grid.colour='NA', grid.minor.colour='NA', axis.colour='black',
> border.colour='NA')
> ggtheme(themePublish)
>
> #Generate the data
> plotdata<-data.frame(x=1:10, y=runif(10))
>
> #Build the ggplot object
>
> ggPlotObject<-ggplot()
> ggPlotObject<-ggPlotObject+layer(data=plotdata,
> mapping=aes_string(x='x',y='y'),geom='point', stat='identity', size=4)
> ggPlotObject<-ggPlotObject+ scale_y_continuous(limits=c(0,1), expand=c(0,0))
>
> #################
> #I define the following function
> F.ggPlot.AddAxisLines<-function(ggPlotObject, col='black', lwd=2,
> lty='solid'){
> ###################################################################################################
> # First, capture the ggPlot object as a gTree
> ggPlotTree<-ggplot_plot(ggPlotObject)
>
> #Get the parameters defined for the lines
> LinePars <- gpar(col=col, lwd=lwd, lty=lty)
>
> #Then, create the objects to add to the axes
> YAxis.Line<-linesGrob(x=unit(c(1,1), 'npc'), y=unit(c(0,1), 'npc'),
> vp='left_axis', name='YAxisLine', gp=LinePars)
> XAxis.Line<-linesGrob(x=unit(c(0,1), 'npc'), y=unit(c(1,1), 'npc'),
> vp='bottom_axis', name='XAxisLine', gp=LinePars)
>
> #Add the grobs to this object
> ggPlotTreeMod<-ggPlotTree
> ggPlotTreeMod<-addGrob(ggPlotTreeMod, YAxis.Line, gPath('yaxis'), grep=TRUE)
> ggPlotTreeMod<-addGrob(ggPlotTreeMod, XAxis.Line, gPath('xaxis'), grep=TRUE)
>
> #Edit the properties of the ticks, to match those of the lines
> ggPlotTreeMod<-editGrob(ggPlotTreeMod, gPath('ticks'), gp=LinePars,
> grep=TRUE, global=TRUE)
>
> #Output the new object
>
> ggPlotTreeMod
> }
>
> # and then do
> ggPlotTreeMod2 <- F.ggPlot.AddAxisLines(ggPlotObject)
> grid.draw(ggPlotTreeMod2)
>
> # I get different line thicknesses for the two axes
>
> #However, if I do
> ggPlotTreeMod3 <- F.ggPlot.AddAxisLines(ggPlotObject, lwd=3)
> grid.draw(ggPlotTreeMod3)
>
> # I get the same line thickness on both axes, but thicker ticks

How are you judging thickness?  It may be that due to the size of
ticks and the size of the plot surface that thickness of the line is
not a whole number of pixels, and depending on the exact position of
the line, gets rounded up or down.  Trying saving as pdf and zooming
in.

Hadley

--
http://had.co.nz/

______________________________________________
[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
|

Re: "lwd" gives different plotted line widths for Vertical and horizontal Lines when lwd= 2?

Pedro Conte de Barros
Hi Hadley,

Thanks for the quick and informative reply.

I did what you suggested, and it was indeed the problem for the axes
lines (both are the same thickness). However, ticks are still
thicker... I am attaching the two pdf files (one with lwd=2, the
other with lwd=3).

Thanks again,
Pedro
At 15:02 2008/07/06, you wrote:

>On Sun, Jul 6, 2008 at 6:33 AM, Pedro de Barros <[hidden email]> wrote:
> > Dear All,
> >
> > I have been trying to add lines to the axis grobs of plots produced with
> > ggplot2.
> >
> > The code I have used is below. It works, although I do not think it is a
> > really elegant way of doing what I want....
> > However, I am now noticing that when plotted, the width of the lines in the
> > axis are not the same as the line widths I get for the ticks (the ticks get
> > thicker lines).  Also, when I build a function to do the same conversion, I
> > get different (thinner) line widths for the x-axis I have been trying to
> > find out why this is so, but I seem to have met a dead-end.
> >
> > #Test
> > library(ggplot2)
> > #Set the basic overall plot options
> > themePublish <-list(background.fill='white', grid.fill='white',
> > grid.colour='NA', grid.minor.colour='NA', axis.colour='black',
> > border.colour='NA')
> > ggtheme(themePublish)
> >
> > #Generate the data
> > plotdata<-data.frame(x=1:10, y=runif(10))
> >
> > #Build the ggplot object
> >
> > ggPlotObject<-ggplot()
> > ggPlotObject<-ggPlotObject+layer(data=plotdata,
> > mapping=aes_string(x='x',y='y'),geom='point', stat='identity', size=4)
> > ggPlotObject<-ggPlotObject+ scale_y_continuous(limits=c(0,1),
> expand=c(0,0))
> >
> > #################
> > #I define the following function
> > F.ggPlot.AddAxisLines<-function(ggPlotObject, col='black', lwd=2,
> > lty='solid'){
> >
> ###################################################################################################
> > # First, capture the ggPlot object as a gTree
> > ggPlotTree<-ggplot_plot(ggPlotObject)
> >
> > #Get the parameters defined for the lines
> > LinePars <- gpar(col=col, lwd=lwd, lty=lty)
> >
> > #Then, create the objects to add to the axes
> > YAxis.Line<-linesGrob(x=unit(c(1,1), 'npc'), y=unit(c(0,1), 'npc'),
> > vp='left_axis', name='YAxisLine', gp=LinePars)
> > XAxis.Line<-linesGrob(x=unit(c(0,1), 'npc'), y=unit(c(1,1), 'npc'),
> > vp='bottom_axis', name='XAxisLine', gp=LinePars)
> >
> > #Add the grobs to this object
> > ggPlotTreeMod<-ggPlotTree
> > ggPlotTreeMod<-addGrob(ggPlotTreeMod, YAxis.Line, gPath('yaxis'),
> grep=TRUE)
> > ggPlotTreeMod<-addGrob(ggPlotTreeMod, XAxis.Line, gPath('xaxis'),
> grep=TRUE)
> >
> > #Edit the properties of the ticks, to match those of the lines
> > ggPlotTreeMod<-editGrob(ggPlotTreeMod, gPath('ticks'), gp=LinePars,
> > grep=TRUE, global=TRUE)
> >
> > #Output the new object
> >
> > ggPlotTreeMod
> > }
> >
> > # and then do
> > ggPlotTreeMod2 <- F.ggPlot.AddAxisLines(ggPlotObject)
> > grid.draw(ggPlotTreeMod2)
> >
> > # I get different line thicknesses for the two axes
> >
> > #However, if I do
> > ggPlotTreeMod3 <- F.ggPlot.AddAxisLines(ggPlotObject, lwd=3)
> > grid.draw(ggPlotTreeMod3)
> >
> > # I get the same line thickness on both axes, but thicker ticks
>
>How are you judging thickness?  It may be that due to the size of
>ticks and the size of the plot surface that thickness of the line is
>not a whole number of pixels, and depending on the exact position of
>the line, gets rounded up or down.  Trying saving as pdf and zooming
>in.
>
>Hadley
>
>--
>http://had.co.nz/

______________________________________________
[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.

TestLwd2.pdf (6K) Download Attachment
TestLwd3.pdf (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: looking for alternative of 'if-else'

Arun.stat
In reply to this post by CTU-2
I am not sure whether I could understand all things. Here is my code :

library(zoo)
# an reproducible example
SD <- 1
date1 <- seq(as.Date("2001-01-01"), as.Date("2002-12-1"), by = "day")
len1 <- length(date1)
set.seed(1) # to make it reproducible
data1 <- zoo(rnorm(len1), date1)
plot(data1)

# calculation for monthly or quarterly mean
calc = function(frequ = c("M S", "Q S"))
              {
               switch(frequ
                       "M S" = aggregate(data1, as.yearmon, mean)
                       "Q S" = aggregate(data1, as.yearqtr, mean))
              }

However I am getting lot of errors while executing. Any further suggestion?

Regards,



On Sun, Jul 6, 2008 at 3:38 PM, <[hidden email]> wrote:

> Why don't you try "switch" Let me assume that you want to calculate a=3 and
> b =5 by using one of "MEAN", "SUM","MIN", and "MAX functions. Then you could
> write your code: (If you want to calculate "MEAN")
>
>  example<-function(fun=c("MEAN", "SUM", "MIN", "MAX")){
>>
> +   fun<-match.arg(fun);a=3;b=5
> +   switch(  fun,
> +            MEAN=(a+b)/2,
> +            SUM=a+b,
> +            MIN=min(a,b),
> +            MAX=max(a,b))
> +            }
>
>>            example("MEAN")
>>
> [1] 4
>
>>
>>  Hope this is helpful,
> Chunhao Tu
>
>
>
>
>
>
> Quoting Arun Kumar Saha <[hidden email]>:
>
>  There is "if-else" loop if I have to choose 1 item from a 2-item list.
>> However if I have a list of 4 items (let say) then how i can choose a
>> single
>> item without employing 'if-else' loop? I mean in VBA I can use
>> "select-case", is there any equivalent in R as well?
>>
>> Regards,
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [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.
>>
>>
>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[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
|

Re: looking for alternative of 'if-else'

Duncan Murdoch
On 06/07/2008 11:58 AM, Arun Kumar Saha wrote:

> I am not sure whether I could understand all things. Here is my code :
>
> library(zoo)
> # an reproducible example
> SD <- 1
> date1 <- seq(as.Date("2001-01-01"), as.Date("2002-12-1"), by = "day")
> len1 <- length(date1)
> set.seed(1) # to make it reproducible
> data1 <- zoo(rnorm(len1), date1)
> plot(data1)
>
> # calculation for monthly or quarterly mean
> calc = function(frequ = c("M S", "Q S"))
>               {
>                switch(frequ
>                        "M S" = aggregate(data1, as.yearmon, mean)
>                        "Q S" = aggregate(data1, as.yearqtr, mean))
>               }

switch() is a function, so you need commas between its arguments, i.e.

switch(frequ,
                         "M S" = aggregate(data1, as.yearmon, mean),
                         "Q S" = aggregate(data1, as.yearqtr, mean))

With that change I see no errors.

Duncan Murdoch


> However I am getting lot of errors while executing. Any further suggestion?
>
> Regards,
>
>
>
> On Sun, Jul 6, 2008 at 3:38 PM, <[hidden email]> wrote:
>
>> Why don't you try "switch" Let me assume that you want to calculate a=3 and
>> b =5 by using one of "MEAN", "SUM","MIN", and "MAX functions. Then you could
>> write your code: (If you want to calculate "MEAN")
>>
>>  example<-function(fun=c("MEAN", "SUM", "MIN", "MAX")){
>> +   fun<-match.arg(fun);a=3;b=5
>> +   switch(  fun,
>> +            MEAN=(a+b)/2,
>> +            SUM=a+b,
>> +            MIN=min(a,b),
>> +            MAX=max(a,b))
>> +            }
>>
>>>            example("MEAN")
>>>
>> [1] 4
>>
>>>  Hope this is helpful,
>> Chunhao Tu
>>
>>
>>
>>
>>
>>
>> Quoting Arun Kumar Saha <[hidden email]>:
>>
>>  There is "if-else" loop if I have to choose 1 item from a 2-item list.
>>> However if I have a list of 4 items (let say) then how i can choose a
>>> single
>>> item without employing 'if-else' loop? I mean in VBA I can use
>>> "select-case", is there any equivalent in R as well?
>>>
>>> Regards,
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> [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.
>>>
>>>
>>
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.

______________________________________________
[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
|

Re: looking for alternative of 'if-else'

CTU-2
In reply to this post by Arun.stat
Hi,
I write the code for you. Hope this helps
> SD <- 1
> date1 <- seq(as.Date("2001-01-01"), as.Date("2002-12-1"), by = "day")
> len1 <- length(date1)
> set.seed(1) # to make it reproducible
> data1 <- zoo(rnorm(len1), date1)
> plot(data1)
>
> # calculation for monthly or quarterly mean
> calc = function(frequ = c("M.S", "Q.S")){
+        frequ<-match.arg(frequ)
+        switch(frequ,
+               M.S = aggregate(data1, as.yearmon, mean),
+               Q.S = aggregate(data1, as.yearqtr, mean))
+               }
>
Take a look what is the difference between my code and yours then you  
will know why your code does not work.

Chunhao Tu





Quoting Arun Kumar Saha <[hidden email]>:

> I am not sure whether I could understand all things. Here is my code :
>
> library(zoo)
> # an reproducible example
> SD <- 1
> date1 <- seq(as.Date("2001-01-01"), as.Date("2002-12-1"), by = "day")
> len1 <- length(date1)
> set.seed(1) # to make it reproducible
> data1 <- zoo(rnorm(len1), date1)
> plot(data1)
>
> # calculation for monthly or quarterly mean
> calc = function(frequ = c("M S", "Q S"))
>               {
>                switch(frequ
>                        "M S" = aggregate(data1, as.yearmon, mean)
>                        "Q S" = aggregate(data1, as.yearqtr, mean))
>               }
>
> However I am getting lot of errors while executing. Any further suggestion?
>
> Regards,
>
>
>
> On Sun, Jul 6, 2008 at 3:38 PM, <[hidden email]> wrote:
>
>> Why don't you try "switch" Let me assume that you want to calculate a=3 and
>> b =5 by using one of "MEAN", "SUM","MIN", and "MAX functions. Then you could
>> write your code: (If you want to calculate "MEAN")
>>
>>  example<-function(fun=c("MEAN", "SUM", "MIN", "MAX")){
>>>
>> +   fun<-match.arg(fun);a=3;b=5
>> +   switch(  fun,
>> +            MEAN=(a+b)/2,
>> +            SUM=a+b,
>> +            MIN=min(a,b),
>> +            MAX=max(a,b))
>> +            }
>>
>>>            example("MEAN")
>>>
>> [1] 4
>>
>>>
>>>  Hope this is helpful,
>> Chunhao Tu
>>
>>
>>
>>
>>
>>
>> Quoting Arun Kumar Saha <[hidden email]>:
>>
>>  There is "if-else" loop if I have to choose 1 item from a 2-item list.
>>> However if I have a list of 4 items (let say) then how i can choose a
>>> single
>>> item without employing 'if-else' loop? I mean in VBA I can use
>>> "select-case", is there any equivalent in R as well?
>>>
>>> Regards,
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> [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.
>>>
>>>
>>
>>
>>
>

______________________________________________
[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
|

Re: "lwd" gives different plotted line widths for Vertical and horizontal Lines when lwd= 2?

hadley wickham
In reply to this post by Pedro Conte de Barros
Hi Pedro,

Hmm, that's probably a bug in the way that ggplot2 is drawing the
lines.  The good news is that the next version of ggplot has a much
more flexible theming system, so to get the same effect as your grid
code, all you'll need to do is:

theme_set(axis.line = theme_segment(colour = "black")
theme_set(axis.ticks = theme_segment(colour = "black", size = 3)

I'm still fixing a lot of bugs in the new code (and documenting how it
all works), but I hope to have another release out in the near future.

Hadley

On Sun, Jul 6, 2008 at 10:06 AM, Pedro de Barros <[hidden email]> wrote:

> Hi Hadley,
>
> Thanks for the quick and informative reply.
>
> I did what you suggested, and it was indeed the problem for the axes lines
> (both are the same thickness). However, ticks are still thicker... I am
> attaching the two pdf files (one with lwd=2, the other with lwd=3).
>
> Thanks again,
> Pedro
> At 15:02 2008/07/06, you wrote:
>>
>> On Sun, Jul 6, 2008 at 6:33 AM, Pedro de Barros <[hidden email]> wrote:
>> > Dear All,
>> >
>> > I have been trying to add lines to the axis grobs of plots produced with
>> > ggplot2.
>> >
>> > The code I have used is below. It works, although I do not think it is a
>> > really elegant way of doing what I want....
>> > However, I am now noticing that when plotted, the width of the lines in
>> > the
>> > axis are not the same as the line widths I get for the ticks (the ticks
>> > get
>> > thicker lines).  Also, when I build a function to do the same
>> > conversion, I
>> > get different (thinner) line widths for the x-axis I have been trying to
>> > find out why this is so, but I seem to have met a dead-end.
>> >
>> > #Test
>> > library(ggplot2)
>> > #Set the basic overall plot options
>> > themePublish <-list(background.fill='white', grid.fill='white',
>> > grid.colour='NA', grid.minor.colour='NA', axis.colour='black',
>> > border.colour='NA')
>> > ggtheme(themePublish)
>> >
>> > #Generate the data
>> > plotdata<-data.frame(x=1:10, y=runif(10))
>> >
>> > #Build the ggplot object
>> >
>> > ggPlotObject<-ggplot()
>> > ggPlotObject<-ggPlotObject+layer(data=plotdata,
>> > mapping=aes_string(x='x',y='y'),geom='point', stat='identity', size=4)
>> > ggPlotObject<-ggPlotObject+ scale_y_continuous(limits=c(0,1),
>> > expand=c(0,0))
>> >
>> > #################
>> > #I define the following function
>> > F.ggPlot.AddAxisLines<-function(ggPlotObject, col='black', lwd=2,
>> > lty='solid'){
>> >
>> > ###################################################################################################
>> > # First, capture the ggPlot object as a gTree
>> > ggPlotTree<-ggplot_plot(ggPlotObject)
>> >
>> > #Get the parameters defined for the lines
>> > LinePars <- gpar(col=col, lwd=lwd, lty=lty)
>> >
>> > #Then, create the objects to add to the axes
>> > YAxis.Line<-linesGrob(x=unit(c(1,1), 'npc'), y=unit(c(0,1), 'npc'),
>> > vp='left_axis', name='YAxisLine', gp=LinePars)
>> > XAxis.Line<-linesGrob(x=unit(c(0,1), 'npc'), y=unit(c(1,1), 'npc'),
>> > vp='bottom_axis', name='XAxisLine', gp=LinePars)
>> >
>> > #Add the grobs to this object
>> > ggPlotTreeMod<-ggPlotTree
>> > ggPlotTreeMod<-addGrob(ggPlotTreeMod, YAxis.Line, gPath('yaxis'),
>> > grep=TRUE)
>> > ggPlotTreeMod<-addGrob(ggPlotTreeMod, XAxis.Line, gPath('xaxis'),
>> > grep=TRUE)
>> >
>> > #Edit the properties of the ticks, to match those of the lines
>> > ggPlotTreeMod<-editGrob(ggPlotTreeMod, gPath('ticks'), gp=LinePars,
>> > grep=TRUE, global=TRUE)
>> >
>> > #Output the new object
>> >
>> > ggPlotTreeMod
>> > }
>> >
>> > # and then do
>> > ggPlotTreeMod2 <- F.ggPlot.AddAxisLines(ggPlotObject)
>> > grid.draw(ggPlotTreeMod2)
>> >
>> > # I get different line thicknesses for the two axes
>> >
>> > #However, if I do
>> > ggPlotTreeMod3 <- F.ggPlot.AddAxisLines(ggPlotObject, lwd=3)
>> > grid.draw(ggPlotTreeMod3)
>> >
>> > # I get the same line thickness on both axes, but thicker ticks
>>
>> How are you judging thickness?  It may be that due to the size of
>> ticks and the size of the plot surface that thickness of the line is
>> not a whole number of pixels, and depending on the exact position of
>> the line, gets rounded up or down.  Trying saving as pdf and zooming
>> in.
>>
>> Hadley
>>
>> --
>> http://had.co.nz/
>



--
http://had.co.nz/

______________________________________________
[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.