[R] Split charts with ggplot2, tidyquant

Eric Berger ericjberger at gmail.com
Sun Jan 21 21:54:24 CET 2018


Hi Charlie and Bert,
Thank you both for the suggestions and pointers. I will look into them.

FYI I repeatedly refer to tidyquant because that package refers to itself as
"tidyquant: Tidy Quantitative Financial Analysis" and I am hoping to get the
attention of someone who is involved in the tidyquant package. The type of split
chart I am interested in is standard / prevalent in financial charting,
e.g. the charts on https://www.bloomberg.com/markets/stocks all have
an 'Indicators' button which allows you add, say, a volume chart as a
subchart below the main part of the chart.

Thanks again,
Eric



On Sun, Jan 21, 2018 at 6:45 PM, Charlie Redmon <redmonc at gmail.com> wrote:
> Thanks for the reminder about lattice! I did some searching and there's a
> good example of manipulating the size of subplots using the `position`
> argument (see pp. 202-203 in the Trellis Users Guide:
> http://ml.stat.purdue.edu/stat695t/writings/Trellis.User.pdf). This is not
> within the paneling environment with the headers like in other trellis plots
> though, so you'll have to do a bit more digging to see how to get that to
> work if you need those headers.
>
>
> Best,
>
> Charlie
>
>
> On 01/20/2018 03:17 PM, Bert Gunter wrote:
>>
>> That (the need for base graphics) is false. It certainly **can** be done
>> in base graphics -- see ?layout for a perhaps more straightforward way to do
>> it along the lines you suggest.
>>
>> However both lattice and ggplot are based on grid graphics, which has a
>> similar but slightly more flexible ?grid.layout function which would allow
>> one to size and place subsequent ggplot or lattice graphs in an arbitrary
>> layout as you have described (iiuc) for the base graphics case.
>>
>> Perhaps even simpler would be to use the "position" argument of the
>> print.trellis() function to locate trellis plots. Maybe ggplot() has
>> something similar.
>>
>> In any case, the underlying grid graphics functionality allows **much**
>> greater fine control of graphical elements (including rotation, for example)
>> -- at the cost of greater complexity. I would agree that doing it from
>> scratch using base grid functions is most likely overkill here, though. But
>> it's there.
>>
>> IMHO only, the base graphics system was great in its time, but its time
>> has passed. Grid graphics is much more powerful because it is objects based
>> -- that is, grid graphs are objects that can be saved, modified, and even
>> interacted with in flexible ways. Lattice and ggplot incarnations take
>> advantage of this, giving them more power and flexibility than the base
>> graphics capabilities can muster.
>>
>> I repeat -- IMHO only! Feel free to disagree. I don't want to start any
>> flame wars here.
>>
>> Cheers,
>> Bert
>>
>>
>>
>>
>>
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along and
>> sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>> On Sat, Jan 20, 2018 at 12:19 PM, Charlie Redmon <redmonc at gmail.com
>> <mailto:redmonc at gmail.com>> wrote:
>>
>>     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
>>     <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/
>>
>> <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>
>>     > <mailto: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>
>>     <http://variable.name>="parameter", # name of new
>>     >     variable for index
>>     > value.name <http://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>>
>>     >         <mailto: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]]
>>
>>     ______________________________________________
>>     R-help at r-project.org <mailto:R-help at r-project.org> mailing list --
>>     To UNSUBSCRIBE and more, see
>>     https://stat.ethz.ch/mailman/listinfo/r-help
>>     <https://stat.ethz.ch/mailman/listinfo/r-help>
>>     PLEASE do read the posting guide
>>     http://www.R-project.org/posting-guide.html
>>     <http://www.R-project.org/posting-guide.html>
>>     and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
> --
> Charles Redmon
> GRA, Center for Research Methods and Data Analysis
> PhD Student, Department of Linguistics
> University of Kansas
> Lawrence, KS, USA
>



More information about the R-help mailing list