Sigurgeir Gunnarsson
2013-Apr-14 11:36 UTC
[R] Problem plotting continuous and discrete series in ggplot with facet
I have data that plots over time with four different variables. I would like to combine them in one plot using facet_grid, where each variable gets its own sub-plot. The following code resembles my data require(ggplot2) require(reshape2) subm <- melt(economics, id='date', c('psavert','uempmed','unemploy')) mcsm <- melt(data.frame(date=economics$date, q=quarters(economics$date)), id='date') mcsm$value <- factor(mcsm$value) ggplot(subm, aes(date, value, col=variable, group=1)) + geom_line() + facet_grid(variable~., scale='free_y') + geom_step(data=mcsm, aes(date, value)) + scale_y_discrete(breaks=levels(mcsm$value)) If I leave out scale_y_discrete, R complains that I'm trying to combine discrete value with continuous scale. If I include scale_y_discreate my continuous series miss their scale. Is there any neat way of solving this issue ? I also see that the legend is alphabetically sorted, can I change that so the legend is ordered like the sub-plots ? --- Regards, Sigurgeir [[alternative HTML version deleted]]
Sigurgeir Gunnarsson
2013-Apr-14 20:54 UTC
[R] Problem plotting continuous and discrete series in ggplot with facet
I'm avare that I'm trying to plot factors and numeric in the same try. What I needed confirmed, before waisting more time seeking the answer, was if this was possible. Now I have confirmed that this is in deed not possible. I have received a really good answer on stackexchange, which I like to share with the list: http://stackoverflow.com/questions/15999304/plotting-continuous-and-discrete-series-in-ggplot-with-facet Regard, Sigurgeir 2013/4/14 Dennis Murphy <djmuser@gmail.com>> Look at head(mcsm): > > > head(mcsm) > date variable value > 1 1967-06-30 q Q2 > 2 1967-07-31 q Q3 > 3 1967-08-31 q Q3 > 4 1967-09-30 q Q3 > 5 1967-10-31 q Q4 > 6 1967-11-30 q Q4 > > I suspect what you're trying to do and it won't work the way you're > doing it. I'd suggest plotting the quarterly data separately and use > the gridExtra package and its grid.arrange() function to put the two > plots on the same graphics page. It will affect the legend, but that's > life. > > Dennis > > On Sun, Apr 14, 2013 at 4:36 AM, Sigurgeir Gunnarsson > <sgunnars@gmail.com> wrote: > > I have data that plots over time with four different variables. I would > > like to combine them in one plot using facet_grid, where each variable > gets > > its own sub-plot. The following code resembles my data > > > > require(ggplot2) > > require(reshape2) > > > > subm <- melt(economics, id='date', c('psavert','uempmed','unemploy')) > > mcsm <- melt(data.frame(date=economics$date, q=quarters(economics$date)), > > id='date') > > mcsm$value <- factor(mcsm$value) > > > > > > ggplot(subm, aes(date, value, col=variable, group=1)) + geom_line() + > > facet_grid(variable~., scale='free_y') + geom_step(data=mcsm, aes(date, > > value)) + scale_y_discrete(breaks=levels(mcsm$value)) > > > > If I leave out scale_y_discrete, R complains that I'm trying to combine > > discrete value with continuous scale. If I include scale_y_discreate my > > continuous series miss their scale. > > > > Is there any neat way of solving this issue ? I also see that the legend > is > > alphabetically sorted, can I change that so the legend is ordered like > the > > sub-plots ? > > > > --- > > Regards, > > Sigurgeir > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org 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. >-- Kv. Sigurgeir [[alternative HTML version deleted]]
Seemingly Similar Threads
- [LLVMdev] Remove redundant code after frame index elimination
- ggplot2 reorder factors for faceting
- Which CDR processing for high load ?
- Extracting selected rows from a matrix to a submatix
- Issue with Asterisk 13, multiple CDR per queue and arbitrary upper limit