Fwd: help displaying all smoother in only one plot, hiding the raw data.

58 views
Skip to first unread message

Antonio P. Ramos

unread,
Jan 29, 2013, 10:58:07 PM1/29/13
to ggp...@googlegroups.com

Hi all,

I use the following to generate the attached graph. 

> qplot(data=subset(w1,ncase>10),y=death.rate,x=maternal_age,group=tablename_x,geom="line",ylim=c(0,.4))  +
+   opts(title="Under 2 deaths rates and Maternal Age by Survey whithin Years \n(minum number of cases by age-survey-year group is 10 children) ") +
+   geom_smooth(aes(group=1)) + facet_wrap(~B2)

In addition to this graph, I wonder if there is any easy to have all smoothers in only one plot, without displaying the raw data. I can send the data, if needed.

 X maternal_age   B2 tablename_x death.rate ncase
1 1            5 1971    IABR23FL        1.0     1
2 2            7 1973    IABR23FL        0.0     1
3 3            7 1975    KEBR33FL        0.0     1
4 4            7 1980    TGBR31FL        0.0     1
5 5            8 1970    TGBR31FL        1.0     1
6 6            8 1975    TGBR31FL        0.5     2

Thanks!

Antonio Pedro. 

5.1.pdf

William Beasley

unread,
Jan 30, 2013, 11:57:19 AM1/30/13
to ggp...@googlegroups.com
Hi Antonio, I don't fully understand the significance of the `tablename_x`, or why  they need to be connected with lines, so be cautious taking my advice.  Here is some code that may get you closer to what you want.  Be especially careful if the variability of the smoothed estimates is being correctly represented.  

When you say, "have all smoothers in only one plot",  I assume you mean "have all smoothers in only one facet/panel"?

ds <- subset(w1, ncase>10)
g <- ggplot(ds, aes(x=material_age, y=death.rate, color=B2))
g <- g + geom_smooth(aes(group=1))
g

Even if this is what you want, I'd still supplement it with a version of the faceted graph.  It can show the variability between years better than a single panel. Consider lightening up the lines using the alpha parameter in geom_line.  This series of graphs is probably getting complicated/serious enough that you need to use `ggplot` plus the geoms, instead of starting with `qplot`.

Consider if there's a way you can reintroduce the cells with low values of ncase, and then reestimate the variability.  But again, I don't understand the sampling structure of the dataset.
-Will Beasley

Antonio P. Ramos

unread,
Jan 31, 2013, 6:01:27 PM1/31/13
to Dennis Murphy, ggp...@googlegroups.com
Thanks for your reply. Unfortunately, the code you kindly provided is not doing the job: instead it is producing different smoothers. To see this consider the plots for these different pieces of code:

# figure 1
qplot(data=subset(w1,ncase>10),y=death.rate,x=maternal_age,group=tablename_x,geom="line",ylim=c(0,.4))  +
  opts(title="Under 2 deaths rates and Maternal Age by Survey whithin Years \n(minum number of cases by age-survey-year group is 10 children) ") +
  geom_smooth(aes(group=1)) + facet_wrap(~B2)

# figure 2
qplot(data=subset(w1,ncase>10),
      y=death.rate, x=maternal_age, group=B2, colour=B2,
      geom="smooth",ylim=c(0,.4),se=FALSE) + 
  opts(title="Under 2 deaths rates and Maternal Age by Survey whithin Years: \nSmoothers")

# fugure 3
ggplot(subset(w1, ncase > 10),
       aes(y=death.rate, x=maternal_age, group=B2, color =B2) ) +
  geom_smooth(se = FALSE) +
  ylim(0, .4)

As we can see, the smoothers from figure one are NOT the same as two and three. Why? 

Thanks

PS: I can send the data, if needed. 
 str(w1)
'data.frame': 56547 obs. of  6 variables:
 $ X           : int  1 2 3 4 5 6 7 8 9 10 ...
 $ maternal_age: int  5 7 7 7 8 8 8 9 9 9 ...
 $ B2          : int  1971 1973 1975 1980 1970 1975 1976 1971 1973 1973 ...
 $ tablename_x : Factor w/ 86 levels "AMBR42FL","BDBR31FL",..: 35 35 41 72 72 72 57 26 38 72 ...
 $ death.rate  : num  1 0 0 0 1 0.5 1 1 0 0 ...
 $ ncase       : int  1 1 1 1 1 2 2 1 1 1 ...


On Wed, Jan 30, 2013 at 11:57 AM, Dennis Murphy <djm...@gmail.com> wrote:
Hi:


On Tue, Jan 29, 2013 at 7:58 PM, Antonio P. Ramos
<ramos.gra...@gmail.com> wrote:
>
> Hi all,
>
> I use the following to generate the attached graph.
>
>>
>> qplot(data=subset(w1,ncase>10),y=death.rate,x=maternal_age,group=tablename_x,geom="line",ylim=c(0,.4))
>> +
> +   opts(title="Under 2 deaths rates and Maternal Age by Survey whithin
> Years \n(minum number of cases by age-survey-year group is 10 children) ") +
> +   geom_smooth(aes(group=1)) + facet_wrap(~B2)

It might look a little better if you used alpha transparency on the
individual lines and maybe a somewhat thinner line width.

>
> In addition to this graph, I wonder if there is any easy to have all
> smoothers in only one plot, without displaying the raw data. I can send the
> data, if needed.

Of course :) Don't facet and use tablename_x as the grouping variable;
you probably want to use it as the color aesthetic, too. Given the
number of smooths involved, I'd suggest dispensing with the confidence
bands. Something like

qplot(data=subset(w1,ncase>10),
           y=death.rate, x=maternal_age, group=tablename_x, color = tablename_x,
           geom="smooth",ylim=c(0,.4))
or
ggplot(subset(w1, ncase > 10),
          aes(y=death.rate, x=maternal_age, group=tablename_x, color =
tablename_x) ) +
     geom_smooth(se = FALSE) +
     ylim(0, 4)

I don't use qplot() very often so I don't know offhand how you'd set
se = FALSE inside the geom_smooth() call.

Dennis

>
>  X maternal_age   B2 tablename_x death.rate ncase
> 1 1            5 1971    IABR23FL        1.0     1
> 2 2            7 1973    IABR23FL        0.0     1
> 3 3            7 1975    KEBR33FL        0.0     1
> 4 4            7 1980    TGBR31FL        0.0     1
> 5 5            8 1970    TGBR31FL        1.0     1
> 6 6            8 1975    TGBR31FL        0.5     2
>
> Thanks!
>
> Antonio Pedro.
>
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

1.pdf
2.pdf
3.pdf

William Beasley

unread,
Feb 1, 2013, 10:56:55 AM2/1/13
to ggp...@googlegroups.com, Dennis Murphy
On Thursday, January 31, 2013 5:01:27 PM UTC-6, Antonio Pedro Ramos wrote:
Thanks for your reply. Unfortunately, the code you kindly provided is not doing the job: instead it is producing different smoothers. To see this consider the plots for these different pieces of code:

You don't want distinct lines for the smoothers? I guess I'm misunderstanding what you mean in your initial post by "I wonder if there is any easy to have all smoothers in only one plot".  You want just one panel and just one smoothing line to represent all the years combined years?
 
As we can see, the smoothers from figure one are NOT the same as two and three. Why? 

Because the `group` is specified differently.  If you want on smoother to represent everything, don't specify a grouping variable for it.

Antonio Pedro Ramos

unread,
Feb 1, 2013, 4:33:44 PM2/1/13
to ggp...@googlegroups.com, Dennis Murphy, ggp...@googlegroups.com
Hi,

Thanks for the replies. 

I would like to have  exactly the same smoothers from figure 1 collected into only one graph, without showing *any* data. I know that the problem is that I am not specifying the group correctly, of course. Indeed, that is the whole point of the post: how can specify the groups correctly so that I can collect the smoother produce by the first chunk of code in only one graph, without the data. 


Best, 


Thanks 

 

Winston Chang

unread,
Feb 1, 2013, 4:54:11 PM2/1/13
to Antonio Pedro Ramos, ggplot2, Dennis Murphy
I'm not 100% sure I understand exactly what you want, but I do think that the smoothers in 2 and 3 were the same as in 1. The only difference is that they're treated as a continuous variable for the purposes of the legend. If you want them to have the same color, you can do something like this:
  ggplot(mtcars, aes(x=wt, y=mpg, group=factor(cyl))) + geom_smooth(se=FALSE)

And if you want them to have different discrete colors, you can do this:
  ggplot(mtcars, aes(x=wt, y=mpg, colour=factor(cyl))) + geom_smooth(se=FALSE)

The factor(cyl) is so that a continuous variable, cyl, is treated as a discrete one for ggplot2.

-Winston




--

Antonio P. Ramos

unread,
Feb 1, 2013, 5:03:11 PM2/1/13
to Winston Chang, ggplot2, Dennis Murphy
Thanks for your response. Yes, I let them to be treated as continious for easy read. Yet they don't look the same. To see this consider the huge inflection in one of the lines close to 1970's for figure 2 and 3. This don't exist for any year in the figure 1, do they? How can one be sure? I think I am getting different smoothers from figure 1 and for 2 and 3 (the last two are the same.)

My very best,

Antonio Pedro.

Winston Chang

unread,
Feb 1, 2013, 5:16:20 PM2/1/13
to Antonio P. Ramos, ggplot2, Dennis Murphy
It just occurred to me that your data set is large enough that it might be switching from loess() (which is used for <=1000 points) to gam() for the smoothing model. What version of ggplot2 are you using? Recent versions (I think 0.9.2 and up) will print a message saying what kind of model is being used -- and they use a slightly different algorithm for determining which model to use.

You can do geom_smooth(method=loess) to force it to use loess in both cases.

-Winston

Antonio P. Ramos

unread,
Feb 1, 2013, 5:35:24 PM2/1/13
to Winston Chang, ggplot2, Dennis Murphy
This detail is kind of important but I don't think it solves the mystery: the below figures were produced by these two pieces of code, where method=loess is fixed; still, I think there are different inflexion points, don't you think? thanks a bunch

> qplot(data=subset(w1,ncase>10),y=death.rate,x=maternal_age,group=tablename_x,geom="line",ylim=c(0,.4))  +
+   opts(title="Under 2 deaths rates and Maternal Age by Survey whithin Years \n(minimum number of cases by age-survey-year group is 10 children) ") +
+   geom_smooth(aes(group=1),method=loess) + facet_wrap(~B2)
There were 50 or more warnings (use warnings() to see the first 50)


> ggplot(subset(w1, ncase > 10),
+        aes(y=death.rate, x=maternal_age, group=B2, color =B2) ) +
+   geom_smooth(se = FALSE,method=loess) +
+   ylim(0, .4)
There were 34 warnings (use warnings() to see them)
fig_1.pdf
fig_2.pdf
Reply all
Reply to author
Forward
0 new messages