How to drop unused factors in faceted R ggplot boxplot?

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

How to drop unused factors in faceted R ggplot boxplot?

George Gorczynski
Below is the data sample(example) that I use to make boxplots. I tried everything I could think of but cannot make a plot where for the south site only years existing in data for this site show. Is what I am trying to do possible? Thank you. Here is my code:

    ggplot(stest, aes(x=year, y=conc)) +
      geom_boxplot(horizontal=TRUE) +
      facet_wrap(~site , ncol=1) +coord_flip() + scale_y_log10()


    site year conc
    south 2001 5.3
    south 2001 4.67
    south 2001 4.98
    south 2002 5.76
    south 2002 5.93
    north 2001 4.64
    north 2001 6.32
    north 2003 11.5
    north 2003 6.3
    north 2004 9.6
    north 2004 56.11
    north 2004 63.55
    north 2004 61.35
    north 2005 67.11
    north 2006 39.17
    north 2006 43.51
    north 2006 76.21
    north 2006 158.89
    north 2006 122.27



Reply | Threaded
Open this post in threaded view
|

Re: How to drop unused factors in faceted R ggplot boxplot?

Jeff Newmiller
Your example is not reproducible. [1]

Usually we use subset() or vector/array indexing when we want to work with part of the data. Apparently you want to exclude rows that specify "north" for which the year does not have a corresponding row with the same year and specifies "south".

southidxs <- with( test, "south" == site )
southyears <- unique( test$year[southidxs] )
subset( test, !("north"==site & !year %in% southyears ))

(untested)

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

Niec <[hidden email]> wrote:

>Below is the data sample(example) that I use to make boxplots. I tried
>everything I could think of but cannot make a plot where for the south
>site
>only years existing in data for this site show. Is what I am trying to
>do
>possible? Thank you. Here is my code:
>
>    ggplot(stest, aes(x=year, y=conc)) +
>      geom_boxplot(horizontal=TRUE) +
>      facet_wrap(~site , ncol=1) +coord_flip() + scale_y_log10()
>
>
>    site year conc
>    south 2001 5.3
>    south 2001 4.67
>    south 2001 4.98
>    south 2002 5.76
>    south 2002 5.93
>    north 2001 4.64
>    north 2001 6.32
>    north 2003 11.5
>    north 2003 6.3
>    north 2004 9.6
>    north 2004 56.11
>    north 2004 63.55
>    north 2004 61.35
>    north 2005 67.11
>    north 2006 39.17
>    north 2006 43.51
>    north 2006 76.21
>    north 2006 158.89
>    north 2006 122.27
>
><http://r.789695.n4.nabble.com/file/n4656480/2013-01-23_2130.png>
>
>
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/How-to-drop-unused-factors-in-faceted-R-ggplot-boxplot-tp4656480.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[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: How to drop unused factors in faceted R ggplot boxplot?

George Gorczynski
Thank you, Jeff. I think I should have been more clear.  I don't want to modify my data. I need to create a series of box plots for different sites (north and south in my example but there are many more) but I don't want empty rows in my plot - for years when data was not collected. I tried to make separate plots and join them with grid.arrange and it plots the layout I want but I can't get the boxes in all plots have uniform look (width), see the screenshot:


And thank you for pointing out the shortcomings of my posted data sample. I'll be better prepared next time.
Reply | Threaded
Open this post in threaded view
|

Re: How to drop unused factors in faceted R ggplot boxplot?

arun kirshna
HI,
You can get the plot in the same window, but the width seems to be a problem.
stest<- read.table(text="
    site year conc
    south 2001 5.3
    south 2001 4.67
    south 2001 4.98
    south 2002 5.76
    south 2002 5.93
    north 2001 4.64
    north 2001 6.32
    north 2003 11.5
    north 2003 6.3
    north 2004 9.6
    north 2004 56.11
    north 2004 63.55
    north 2004 61.35
    north 2005 67.11
    north 2006 39.17
    north 2006 43.51
    north 2006 76.21
    north 2006 158.89
    north 2006 122.27
",sep="",header=T,stringsAsFactors=T)
stest1<-stest
 stest1<- within(stest1,{year<- factor(year)})
ggplot(stest1, aes(x=year, y=conc)) + geom_boxplot(horizontal=TRUE)+
     facet_wrap(~ site, ncol = 1, scales="free_x") + coord_flip()+scale_y_log10()+
  theme_bw() + opts(strip.text.x = theme_text())
A.K.





----- Original Message -----
From: Niec <[hidden email]>
To: [hidden email]
Cc:
Sent: Thursday, January 24, 2013 1:13 PM
Subject: Re: [R] How to drop unused factors in faceted R ggplot boxplot?

Thank you, Jeff. I think I should have been more clear.  I don't want to
modify my data. I need to create a series of box plots for different sites
(north and south in my example but there are many more) but I don't want
empty rows in my plot - for years when data was not collected. I tried to
make separate plots and join them with grid.arrange and it plots the layout
I want but I can't get the boxes in all plots have uniform look (width), see
the screenshot:
<http://r.789695.n4.nabble.com/file/n4656535/2013-01-24_0949.png>

And thank you for pointing out the shortcomings of my posted data sample.
I'll be better prepared next time.



--
View this message in context: http://r.789695.n4.nabble.com/How-to-drop-unused-factors-in-faceted-R-ggplot-boxplot-tp4656480p4656535.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[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: How to drop unused factors in faceted R ggplot boxplot?

Gorczynski, George
Hi and thanks. This unfortunately doesn't work because:
"ggplot2 does not currently support free scales with a non-cartesian coord or coord_flip."

-----Original Message-----
From: arun [mailto:[hidden email]]
Sent: January-24-13 1:49 PM
To: Gorczynski, George
Cc: R help
Subject: Re: [R] How to drop unused factors in faceted R ggplot boxplot?

HI,
You can get the plot in the same window, but the width seems to be a problem.
stest<- read.table(text="
    site year conc
    south 2001 5.3
    south 2001 4.67
    south 2001 4.98
    south 2002 5.76
    south 2002 5.93
    north 2001 4.64
    north 2001 6.32
    north 2003 11.5
    north 2003 6.3
    north 2004 9.6
    north 2004 56.11
    north 2004 63.55
    north 2004 61.35
    north 2005 67.11
    north 2006 39.17
    north 2006 43.51
    north 2006 76.21
    north 2006 158.89
    north 2006 122.27
",sep="",header=T,stringsAsFactors=T)
stest1<-stest
 stest1<- within(stest1,{year<- factor(year)}) ggplot(stest1, aes(x=year, y=conc)) + geom_boxplot(horizontal=TRUE)+
     facet_wrap(~ site, ncol = 1, scales="free_x") + coord_flip()+scale_y_log10()+
  theme_bw() + opts(strip.text.x = theme_text()) A.K.





----- Original Message -----
From: Niec <[hidden email]>
To: [hidden email]
Cc:
Sent: Thursday, January 24, 2013 1:13 PM
Subject: Re: [R] How to drop unused factors in faceted R ggplot boxplot?

Thank you, Jeff. I think I should have been more clear.  I don't want to
modify my data. I need to create a series of box plots for different sites
(north and south in my example but there are many more) but I don't want
empty rows in my plot - for years when data was not collected. I tried to
make separate plots and join them with grid.arrange and it plots the layout
I want but I can't get the boxes in all plots have uniform look (width), see
the screenshot:
<http://r.789695.n4.nabble.com/file/n4656535/2013-01-24_0949.png>

And thank you for pointing out the shortcomings of my posted data sample.
I'll be better prepared next time.



--
View this message in context: http://r.789695.n4.nabble.com/How-to-drop-unused-factors-in-faceted-R-ggplot-boxplot-tp4656480p4656535.html
Sent from the R help mailing list archive at Nabble.com.

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