[R] ggplot2, geom_hline and facet_grid

Sandy Small sandy.small at nhs.net
Thu Jan 20 12:46:08 CET 2011


   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
     ________________________________________
     From: [3]h.wickham at gmail.com [[4]h.wickham at gmail.com] On Behalf Of Hadley
     Wickham [[5]hadley at rice.edu]
     Sent: 19 January 2011 15:11
     To: Small Sandy (NHS Greater Glasgow & Clyde)
     Cc: [6]r-help at r-project.org

   Subject: Re: [R] ggplot2, geom_hline and facet_grid

   Hi Sandy,
   It's difficult to know what's going wrong without a small reproducible
   example ([7]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 <[8]sandy.small at nhs.net> wrote:
   > 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) <[9]sandy.small at nhs.net <[10]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}}
   >
   > ______________________________________________
   > [11]R-help at r-project.org mailing list
   > [12]https://stat.ethz.ch/mailman/listinfo/r-help
   > PLEASE do read the posting guide
   [13]http://www.R-project.org/posting-guide.html
   > and provide commented, minimal, self-contained, reproducible code.
   >
   --
   Assistant Professor / Dobelman Family Junior Chair
   Department of Statistics / Rice University
   [14]http://had.co.nz/
   ****************************************************************************
   ****************************************

   This  message may contain confidential information. If you are not the
   intended recipient please inform the
   sender that you have received the message in error before deleting it.
   Please do not disclose, copy or distribute information in this e-mail or
   take any action in reliance on its contents:
   to do so is strictly prohibited and may be unlawful.
   Thank you for your co-operation.
   NHSmail is the secure email and directory service available for all NHS
   staff in England and Scotland
   NHSmail  is  approved  for exchanging patient data and other sensitive
   information with NHSmail and GSi recipients
   NHSmail provides an email address for your career in the NHS and can be
   accessed anywhere
   For  more  information  and  to  find  out  how  you can switch, visit
   [15]www.connectingforhealth.nhs.uk/nhsmail

   ______________________________________________
   [16]R-help at r-project.org mailing list
   [17]https://stat.ethz.ch/mailman/listinfo/r-help
   PLEASE do read the posting guide
   [18]http://www.R-project.org/posting-guide.html
   and provide commented, minimal, self-contained, reproducible code.

--

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

Phone (Glasgow): 01412114592
Phone (Forth Valley): 01324567002
E-mail: [19]Sandy.Small at NHS.net

   ****************************************************************************
   ****************************************
   This  message may contain confidential information. If you are not the
   intended recipient please inform the
   sender that you have received the message in error before deleting it.
   Please do not disclose, copy or distribute information in this e-mail or
   take any action in reliance on its contents:
   to do so is strictly prohibited and may be unlawful.
   Thank you for your co-operation.
   NHSmail is the secure email and directory service available for all NHS
   staff in England and Scotland
   NHSmail  is  approved  for exchanging patient data and other sensitive
   information with NHSmail and GSi recipients
   NHSmail provides an email address for your career in the NHS and can be
   accessed anywhere
   For  more  information  and  to  find  out  how  you can switch, visit
   www.connectingforhealth.nhs.uk/nhsmail
   ****************************************************************************
   ****************************************

References

   1. mailto:sandy.small at nhs.net
   2. https://gist.github.com/786894
   3. mailto:h.wickham at gmail.com
   4. mailto:h.wickham at gmail.com
   5. mailto:hadley at rice.edu
   6. mailto:r-help at r-project.org
   7. https://github.com/hadley/devtools/wiki/Reproducibility
   8. mailto:sandy.small at nhs.net
   9. mailto:sandy.small at nhs.net
  10. mailto:ailto%3Asandy.small at nhs.net
  11. mailto:R-help at r-project.org
  12. https://stat.ethz.ch/mailman/listinfo/r-help
  13. http://www.R-project.org/posting-guide.html
  14. http://had.co.nz/
  15. http://www.connectingforhealth.nhs.uk/nhsmail
  16. mailto:R-help at r-project.org
  17. https://stat.ethz.ch/mailman/listinfo/r-help
  18. http://www.R-project.org/posting-guide.html
  19. mailto:Sandy.Small at NHS.net


More information about the R-help mailing list