[R] Split charts with ggplot2, tidyquant

Charlie Redmon redmonc at gmail.com
Sat Jan 20 21:19:09 CET 2018


For this kind of control you will probably need to move to base graphics 
and utilize the `fig` argument in par(), in which case you would want to 
run the plot() command twice: once with your first outcome and once with 
your second, changing the par() settings before each one to control the 
size.


On 01/19/2018 01:39 PM, Eric Berger wrote:
> Hi Charlie,
> Thanks. This is helpful. As mentioned in my original question, I want 
> to be able to plot a few such charts on the same page,
> say a 2 x 2 grid with such a chart for each of 4 different stocks. 
> Using your solution I accomplished this by making
> a list pLst of your ggplots and then calling cowplot::plot_grid( 
> plotlist=pLst, nrow=2, ncol=2 )  That worked fine.
>
> The one issue  I have is that in the ggplot you suggest, the price and 
> volume facets are the same size. I would like them to be different sizes
> (e.g. the volume facet at the bottom is generally shown smaller than 
> the facet above it in these types of charts.)
>
> I tried to find out how to do it but didn't succeed. I found a couple 
> of relevant discussions (including Hadley writing that he did not 
> think it was a useful feature. :-()
>
> https://github.com/tidyverse/ggplot2/issues/566
>
> and an ancient one where someone seems to have been able to get a 
> heights parameter working in a call to facet_grid but it did not work 
> for me.
> https://kohske.wordpress.com/2010/12/25/adjusting-the-relative-space-of-a-facet-grid/
>
> Thanks again,
> Eric
>
> p.s. Joshua thanks for your suggestions, but I was hoping for a ggplot 
> solution.
>
>
> On Fri, Jan 19, 2018 at 6:33 PM, Charlie Redmon <redmonc at gmail.com 
> <mailto:redmonc at gmail.com>> wrote:
>
>     So the general strategy for getting these into separate panels in
>     ggplot is to have a single variable that will be your response and
>     a factor variable that indexes which original variable it came
>     from. This can be accomplished in many ways, but the way I use is
>     with the melt() function in the reshape2 package.
>     For example,
>
>     library(reshape2)
>     plotDF <- melt(SPYdf,
>                             id.vars="Date", # variables to replicate
>                             measure.vars=c("close", "volume"), #
>     variables to create index from
>     variable.name <http://variable.name>="parameter", # name of new
>     variable for index
>     value.name <http://value.name>="resp") # name of what will be your
>     response variable
>
>     Now the ggplot2 code:
>
>     library(ggplot2)
>     ggplot(plotDF, aes(x=Date, y=resp)) +
>         facet_wrap(~parameter, ncol=1, scales="free") +
>         geom_line()
>
>
>     Hope that does the trick!
>
>     Charlie
>
>
>
>     On 01/18/2018 02:11 PM, Eric Berger wrote:
>
>         Hi Charlie,
>         I am comfortable to put the data in any way that works best.
>         Here are two possibilities: an xts and a data frame.
>
>         library(quantmod)
>         quantmod::getSymbols("SPY")  # creates xts variable SPY
>         SPYxts <- SPY[,c("SPY.Close","SPY.Volume")]
>         SPYdf  <-
>         data.frame(Date=index(SPYxts),close=as.numeric(SPYxts$SPY.Close),
>          volume=as.numeric(SPYxts$SPY.Volume))
>         rownames(SPYdf) <- NULL
>
>         head(SPYxts)
>         head(SPYdf)
>
>         #           SPY.Close SPY.Volume
>         #2007-01-03    141.37   94807600
>         #2007-01-04    141.67   69620600
>         #2007-01-05    140.54   76645300
>         #2007-01-08    141.19   71655000
>         #2007-01-09    141.07   75680100 <tel:07%C2%A0%20%C2%A075680100>
>         #2007-01-10    141.54   72428000
>
>         #        Date  close   volume
>         #1 2007-01-03 141.37 94807600
>         #2 2007-01-04 141.67 69620600
>         #3 2007-01-05 140.54 76645300
>         #4 2007-01-08 141.19 71655000
>         #5 2007-01-09 141.07 75680100 <tel:07%2075680100>
>         #6 2007-01-10 141.54 72428000
>
>         Thanks,
>         Eric
>
>
>
>         On Thu, Jan 18, 2018 at 8:00 PM, Charlie Redmon
>         <redmonc at gmail.com <mailto:redmonc at gmail.com>
>         <mailto:redmonc at gmail.com <mailto:redmonc at gmail.com>>> wrote:
>
>             Could you provide some information on your data structure
>         (e.g.,
>             are the two time series in separate columns in the data)? The
>             solution is fairly straightforward once you have the data
>         in the
>             right structure. And I do not think tidyquant is necessary for
>             what you want.
>
>             Best,
>             Charlie
>
>             --     Charles Redmon
>             GRA, Center for Research Methods and Data Analysis
>             PhD Student, Department of Linguistics
>             University of Kansas
>             Lawrence, KS, USA
>
>
>
>     -- 
>     Charles Redmon
>     GRA, Center for Research Methods and Data Analysis
>     PhD Student, Department of Linguistics
>     University of Kansas
>     Lawrence, KS, USA
>
>

-- 
Charles Redmon
GRA, Center for Research Methods and Data Analysis
PhD Student, Department of Linguistics
University of Kansas
Lawrence, KS, USA


	[[alternative HTML version deleted]]



More information about the R-help mailing list