Skip to content

ggplot2, geom_hline and facet_grid

6 messages · Sandy Small, Hadley Wickham, Small Sandy (NHS Greater Glasgow & Clyde) +1 more

#
Having upgraded to R version 2.12.1 I still have the same problem:

The combination of facet_grid and geom_hline produce (for me) 4 panels
of which two are empty of any data or lines (labelled "1" and "2").
Removing either the facet_grid or the geom_hline  gives me the result I
would then expect.

I have tried forcing the rhythm to be a factor
Anyone have any ideas?

Sandy
Dennis Murphy wrote:
Hi:

    The attached plot comes from the following code:

    g <- ggplot(data =f, aes(x = (variable_time + fixed_time)/2, y
    variable_time - fixed_time))
    g + geom_point() + geom_hline(yintercept =) + facet_grid(ecd_rhythm ~ .)

    Is this what you were expecting?

     > sessionInfo()
    R version 2.12.1 Patched (2010-12-18 r53869)
    Platform: x86_64-pc-mingw32/x64 (64-bit)

    locale:
    [1] LC_COLLATE=glish_United States.1252
    [2] LC_CTYPE=glish_United States.1252
    [3] LC_MONETARY=glish_United States.1252
    [4] LC_NUMERIC=nbsp;
    [5] LC_TIME=glish_United States.1252

    attached base packages:
    [1] splines   stats     graphics  grDevices utils     datasets
    grid
    [8] methods   base

    other attached packages:
     [1] data.table_1.5.1 doBy_4.2.2       R2HTML_2.2       contrast_0.13
     [5] Design_2.3-0     Hmisc_3.8-3      survival_2.36-2  sos_1.3-0
     [9] brew_1.0-4       lattice_0.19-17  ggplot2_0.8.9    proto_0.3-8
    [13] reshape_0.8.3    plyr_1.4

    loaded via a namespace (and not attached):
    [1] cluster_1.13.2     digest_0.4.2       Matrix_0.999375-46
    reshape2_1.1
    [5] stringr_0.4        tools_2.12.1

    HTH,
    Dennis

    On Tue, Jan 18, 2011 at 1:46 AM, Small Sandy (NHS Greater Glasgow &
Clyde) <sandy.small at nhs.net <ailto:sandy.small at nhs.net%22>> wrote:
Hi

        I have a long data set on which I want to do Bland-Altman style
        plots for each rhythm type
        Using ggplot2, when I use geom_hline with facet_grid I get an
        extra set of empty panels.
        I can't get it to do it with the "Diamonds" data supplied with
        the package so here is a (much abbreviated) example:

         > lvexs
          cvd_basestudy ecd_rhythm fixed_time variable_time
        1       CBP05J02         AF    30.9000       29.4225
        2       CBP05J02         AF    33.1700       32.0350
        3       CBP05J02         AF    32.5700       30.2775
        4       CBP05J02         AF    32.0550       33.7275
        5       CBP05J02      SINUS    30.9175       28.3475
        6       CBP05J02      SINUS    30.5725       29.7450
        7       CBP05J02      SINUS    33.0000       31.1550
        9       CBP05J02      SINUS    31.8350       30.7000
        10      CBP05J02      SINUS    34.0450       33.4800
        11      CBP05J02      SINUS    31.3975       29.8150
         >  qplot((variable_time + fixed_time)/2, variable_time -
        fixed_time, data=exs) + facet_grid(ecd_rhythm ~ .) +
        geom_hline(yintercept=0)

        If I take out the geom_hline I get the plots I would expect.

        It doesn't seem to make any difference if I get the mean and
        difference separately.

        Can anyone explain this and tell me how to avoid it (and why
        does it work with the Diamonds data set?

        Any help much appreciated - thanks.

        Sandy

        Sandy Small
        Clinical Physicist
        NHS Forth Valley
        and
        NHS Greater Glasgow and Clyde



********************************************************************************************************************

This message may contain confidential information. If yo...{{dropped:21}}
#
Hi Sandy,

It's difficult to know what's going wrong without a small reproducible
example (https://github.com/hadley/devtools/wiki/Reproducibility) -
could you please provide one?  You might also have better luck with an
email directly to the ggplot2 mailing list.

Hadley
On Wed, Jan 19, 2011 at 2:57 AM, Sandy Small <sandy.small at nhs.net> wrote:

  
    
#
Hi

Still having problems in that when I use geom_hline and facet_grid together I get two extra empty panels

A reproducible example can be found at:

https://gist.github.com/786894

Sandy Small
#
Thank you.
   That seems to work - also on my much larger data set.
   I'm not sure I understand why it has to be defined as a factor, but if it
   works...
   Sandy
Dennis Murphy wrote:
Hi Sandy:
     I can reproduce your problem given the data provided. When I change
     ecd_rhythm from character to factor, it works as you intended.
     > str(lvefeg)
     List of 4     ### Interesting...
      $ cvd_basestudy: chr [1:10] "CBP05J02" "CBP05J02" "CBP05J02" "CBP05J02"
     ...
      $ ecd_rhythm   : chr [1:10] "AF" "AF" "AF" "AF" ...
      $ fixed_time   : num [1:10] 30.9 33.2 32.6 32.1 30.9 ...
      $ variable_time: num [1:10] 29.4 32 30.3 33.7 28.3 ...
      - attr(*, "row.names")= int [1:10] 1 2 3 4 5 6 7 9 10 11
     > class(lvefeg)
     [1] "cast_df"    "data.frame"
     lvefeg$ecd_rhythm <- factor(lvefeg$ecd_rhythm)
     p <- qplot((variable_time + fixed_time) /2 , variable_time - fixed_time,
     data = lvefeg, geom='point')
     p
     p + facet_grid(ecd_rhythm ~ .) + geom_hline(yintercept=0)
     Does that work on your end?  (And thank you for the reproducible example.
     Using dput() allows us to see what you see, which is very helpful.)
     HTH,
     Dennis

   On Wed, Jan 19, 2011 at 1:30 PM, Small Sandy (NHS Greater Glasgow & Clyde)
<[1]sandy.small at nhs.net> wrote:
Hi
     Still  having problems in that when I use geom_hline and facet_grid
     together I get two extra empty panels
     A reproducible example can be found at:
     [2]https://gist.github.com/786894
     Sandy Small
#
Ok, that's a known bug:
https://github.com/hadley/ggplot2/issues/labels/facet#issue/96

Thanks for the reproducible example though!

Hadley
On Thu, Jan 20, 2011 at 3:46 AM, Sandy Small <sandy.small at nhs.net> wrote: