Split charts with ggplot2, tidyquant

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Split charts with ggplot2, tidyquant

Eric Berger
A very common chart in the financial markets is a split chart with two time
series shown in two vertically stacked sub-charts.
A classic case would be the top panel showing the time series of historical
prices of some stock, and the bottom
panel showing the volume traded per day immediately below it. The common
x-axis is the dates of the time period covered.

I would like to create such a standard plot using ggplot2. How does one do
it?
The goals of the tidyquant package would seem to include the easy creation
of such a chart, but I could not find this in tidyquant.

Suppose it were possible to easily create such a chart in ggplot2 (or
tidyquant which uses ggplot2.)
Then with such data for numerous stocks (or other financial instruments)
one could see a grid of such charts by faceting with respect to the stock.

Thanks for any help,

Eric

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Split charts with ggplot2, tidyquant

redmonc
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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Charles Redmon
PhD Student
Department of Linguistics
University of Kansas
Lawrence, KS, USA
Reply | Threaded
Open this post in threaded view
|

Re: Split charts with ggplot2, tidyquant

Eric Berger
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
#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
#6 2007-01-10 141.54 72428000

Thanks,
Eric



On Thu, Jan 18, 2018 at 8:00 PM, Charlie Redmon <[hidden email]> 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
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Split charts with ggplot2, tidyquant

Joshua Ulrich
If you don't want to wait for a ggplot2 solution, here are two
alternatives you can use right now:

chartSeries(SPYxts)
# or (with xts > 0.10
plot(SPYxts$SPY.Close)
addSeries(SPYxts$SPY.Volume, type = "h")

You might also try autoplot.zoo(), though I've never used it.




On Thu, Jan 18, 2018 at 2:11 PM, Eric Berger <[hidden email]> 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
> #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
> #6 2007-01-10 141.54 72428000
>
> Thanks,
> Eric
>
>
>
> On Thu, Jan 18, 2018 at 8:00 PM, Charlie Redmon <[hidden email]> 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
>>
>>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.



--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com
R/Finance 2018 | www.rinfinance.com

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Split charts with ggplot2, tidyquant

redmonc
In reply to this post by Eric Berger
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="parameter", # name of new
variable for index
                         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
> #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
> #6 2007-01-10 141.54 72428000
>
> Thanks,
> Eric
>
>
>
> On Thu, Jan 18, 2018 at 8:00 PM, Charlie Redmon <[hidden email]
> <mailto:[hidden email]>> 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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Charles Redmon
PhD Student
Department of Linguistics
University of Kansas
Lawrence, KS, USA
Reply | Threaded
Open this post in threaded view
|

Re: Split charts with ggplot2, tidyquant

Eric Berger
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 <[hidden email]> 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="parameter", # name of new variable
> for index
>                         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.Cl
>> ose),
>>  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
>> #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
>> #6 2007-01-10 141.54 72428000
>>
>> Thanks,
>> Eric
>>
>>
>>
>> On Thu, Jan 18, 2018 at 8:00 PM, Charlie Redmon <[hidden email]
>> <mailto:[hidden email]>> 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
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Split charts with ggplot2, tidyquant

redmonc
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 <[hidden email]
> <mailto:[hidden email]>> 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
>         <[hidden email] <mailto:[hidden email]>
>         <mailto:[hidden email] <mailto:[hidden email]>>> 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]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Charles Redmon
PhD Student
Department of Linguistics
University of Kansas
Lawrence, KS, USA
Reply | Threaded
Open this post in threaded view
|

Re: Split charts with ggplot2, tidyquant

Bert Gunter-2
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 <[hidden email]> 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
> >
> > 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 <[hidden email]
> > <mailto:[hidden email]>> 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
> >         <[hidden email] <mailto:[hidden email]>
> >         <mailto:[hidden email] <mailto:[hidden email]>>> 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]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Split charts with ggplot2, tidyquant

redmonc
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 <[hidden email]
> <mailto:[hidden email]>> 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
>     <[hidden email] <mailto:[hidden email]>
>     > <mailto:[hidden email] <mailto:[hidden email]>>> 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
>     >         <[hidden email] <mailto:[hidden email]>
>     <mailto:[hidden email] <mailto:[hidden email]>>
>     >         <mailto:[hidden email] <mailto:[hidden email]>
>     <mailto:[hidden email] <mailto:[hidden email]>>>> 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]]
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> 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

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Charles Redmon
PhD Student
Department of Linguistics
University of Kansas
Lawrence, KS, USA
Reply | Threaded
Open this post in threaded view
|

Re: Split charts with ggplot2, tidyquant

Eric Berger
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 <[hidden email]> 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 <[hidden email]
>> <mailto:[hidden email]>> 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
>>     <[hidden email] <mailto:[hidden email]>
>>     > <mailto:[hidden email] <mailto:[hidden email]>>> 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
>>     >         <[hidden email] <mailto:[hidden email]>
>>     <mailto:[hidden email] <mailto:[hidden email]>>
>>     >         <mailto:[hidden email] <mailto:[hidden email]>
>>     <mailto:[hidden email] <mailto:[hidden email]>>>> 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]]
>>
>>     ______________________________________________
>>     [hidden email] <mailto:[hidden email]> 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
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.