changing the side of bottom facet of graph in chart_Series quantmod - r

I would like to know if i can increase the size of the bottom pane of the graph on the plot function chart_Series()
chart_Series(x$A, TA="add_TA(x$B)")
you don't need data to know what this will look like...

It is possible to modify some aspects of chart_Series using the pars and theme objects you can optionally pass to chart_Series. But I don't know if there is a way to feed in modifying the size of the y axis in add_TA etc without directly modifying the source code for add_TA. This is what I've done before, which is a bit messy, but works ... modify the source code.
The line in add_TA you want to modify is this, which is hard coded as (approximatly line 61 of add_TA):
plot_object$add_frame(ylim = range(na.omit(xdata)),
asp = 1)
Changing that line to this (the value of asp (aspect?) is changed), will give you something like what you want:
plot_object$add_frame(ylim = range(na.omit(xdata)),
asp = 3)
This change gives:
getSymbols("AAPL")
chart_Series(AAPL["2016"])
my_add_TA(SMA(AAPL["2016", 4])) #my_add_TA is add_TA with asp line changed
If you're unsure about how to modify the source code of a package, you could follow my answer to a related question here modify chart_Series source on modifying chart_Series as one approach. Another approach is just to recompile the source code for the package with your modifications.

Related

Is there a configuration file like matplotlibrc in Plots.jl?

I try to unify my figures formats across different files. I do not want to use PyPlot.jl.
Is there a configuration file as matplotlibrc in Python? Or some other alternative way that can make me to write these format into a file without changing much about my figures code.
If I understand your question correctly (I haven't used matplotlib in ages, so not sure what the matplotlibrc file exactly does) you want to specify default settings for your plots in Plots.jl.
When using Plots in a running Julia session, you can use the default function:
julia> default(color = "red", linewidth = 10)
sets default arguments for subsequent plot commands.
If you want these defaults to be automatically available in every Julia session you can create a startup file at ~/.julia/config/startup.jl and set an environment variable there:
PLOTS_DEFAULTS = Dict(:color=> 10, :linewidth => 2)
will have the same effect as the call to default above, but will happen automatically once you start Julia.
This is discussed in the docs here.

customise R help files - font colouring

I'm wondering is it possible to customise R help files so that certain text is colour coded and easier to read. rdoc already does this except that it sends the output to the console. I would instead, like it to be sent to the help panel (i'm using Rstudio). Is there any workaround for this?
If we run ?lm normally, we can see the usual help file in the help panel on the right below but when you do it again after using rdoc in Rstudio we get the help file colour coded which is great but its sent to the console output (left side). Ideally, we would like it to remain on display in the help panel as we are running code. The way it is now - it disappears the minute you run something.
?lm
#devtools::install_github("mdequeljoe/rdoc")
library(rdoc)
options(rdoc.by_section = FALSE)
rdoc(lm)
I want to put the code into my .rprofile similar to #csgillespie .rprofile. Note, if you follow his code you can use ?lm instead of having to call rdoc(lm) directly to produce the colour coded console output.
I have a feeling this can't be done easily (if at all?) but interested to hear any suggestions.
Thanks
This is possible, but a little involved. You'll need your own css file defined to do it, though it would be possible to create a function that writes appropriate css.
As a proof of concept, I have used a copy of the "R.css" file defined inside every package's "/html" folder, and just changed the h2 color to red, with the file saved locally as "my.css".
Anyway, once you have the css file, this function will show the appropriate help file with the appropriate styling in your R viewer window:
help_css <- function(func, css_file)
{
pack <- sub("^.*:(.*)>$", "\\1", capture.output(environment(func)))
func <- deparse(substitute(func))
x <- readLines(paste0(find.package(pack), "/html/", func, ".html"))
x[3] <- paste("<style>",
paste(readLines(css_file), collapse = "\n"),
"</style>")
writeLines(x, file.path(tempdir(), "test.html"))
myViewer <- getOption("viewer")
myViewer(file.path(tempdir(), "test.html"))
}
So, for example, if I do:
help_css(lm, "my.css")
I get:
As of RStudio v1.2 you can style RStudio's integrated help pane by creating a custom user theme (essentially an .rstheme file).
I've given help pane styling a try in extending the rscodeio theme (without colored syntax highlighting, though). The latest CSS code is found here.
The help pane styling is currently only available in the optional Tomorrow Night Bright (rscodeio) editor theme.
To use it right away, you can either
install the current rscodeio master branch using remotes:
remotes::install_github("anthonynorth/rscodeio")
And then activating the editor theme named Tomorrow Night Bright (rscodeio) under Tools → Global Options… → Appearance → Editor theme. A first attempt of the help pane CSS code is included.
or – recommended – install my fork's interim-merge branch which contains all my latest work[1] overhauling the package, including a new apply_theme parameter to activate the desired editor theme right away:
remotes::install_github("salim-b/rscodeio#interim-merge")
rscodeio::install_themes(apply_theme = "Tomorrow Night Bright (rscodeio)")
[1]: This has also been proposed upstream a while ago (1, 2) but I haven't heard back from the author since.
The result looks as follows (example for ?pal::as_string):

issues of wrapping up a set of workable R commands into a function

I generate a dendrogam using a collection of r commands. It worked just fine and saved the generated dendromgram into a PDF file. To improve efficiency, I wrapped these commands as a function, which does not change anything. However, the pdf is just a blank file without any graphical content. Please let me know what’s wrong with my function defintion. Thanks.
myplot<-function(inputcsv, outputfile){
library(ggdendro)
library(ggplot2)
x<-read.csv(inputcsv,header=TRUE)
d<-as.dist(x,diag=FALSE,upper=FALSE)
hc<-hclust(d,"ave")
dhc<-as.dendrogram(hc)
ddata<-dendro_data(dhc,type="rectangle")
ddata$labels$text <- gsub("\\."," ",ddata$labels$text)
ggplot(segment(ddata))+geom_segment(aes(x=x0,y=y0,xend=x1,yend=y1))
pdf(outputfile, width=30,height=35)
last_plot()
dev.off()
}
R FAQ
Wrap your ggplot call in a print() function.
ggplot and friends return an object, and the plotting only happens when the object is printed. When you do this on the command line the printing happens automatically. When you stick it in a script or function you have to do it yourself.
The debate on whether this is a good idea or a dumb thing that just generates questions like this continues...

"object not found" error when creating a new geom for a package

Full disclosure: this issue is duplicated on the ggplot2 google group
I'm developing a package that makes heavy use of ggplot2. I've created my own geom—geom_rug_alt—as a way of putting rug fringes on the top/right of the plot instead of the default locations.
My problem is that when geom_rug_alt() is defined and called within a single script, it seems to plot just fine. (Please try it yourself to verify that.) But, in my package geom_rug_alt() is defined in one file (CommonFunctions.R) and called in another (the Residuals() function of larger function foo.R). When I call foo.R on something, I get this error:
Error in geom_rug_alt(aes(x = NULL, y = within.group.residuals, color = factor(within.1.sd.of.the.mean.of.all.residuals)), :
object 'GeomRugAlt' not found
Now, I've done a couple of things (suggested by Hadley in this thread) to try to make sure that geom_rug_alt() should work properly within the package:
I define GeomRugAlt as a proto object in a file essentially called CommonFunctions.R within my package. CommonFunctions.R contains lines 3-42 of my example script.
In CommonFunctions.R, I was sure to include the build_accessor() line for geom_rug_alt (line 42 in my example script) after the definition of GeomRugAlt
In the package DESCRIPTION file, I have a collate: line where CommonFunctions.R appears first
In the package DESCRIPTION file, I have a LazyLoad: false line
In CommonFunctions.R, I included a require(ggplot2) call before defining GeomRugAlt as a proto object.
In foo.R, I included a require(ggplot2) call before calling geom_rug_alt() within Residuals().
I'm not sure what else I'm missing. Given that my example script runs just fine, I suspect the issue isn't that my geom doesn't work, but that I'm doing something wrong as part of the package development process.
Sorry for duplicating the issue, but I can't seem to find a thorough solution to the problem :-(
Put export(GeomRugAlt) in the NAMESPACE file.

How to source file.R without output

Is it possible to source a file without printing all the charts etc (already tried with echo=F)?
In my case I call the png("filename%03d.png") device early in the script. It is not to big a hassle to comment this out - but all the charts do take a lot of time to render. (the specific file I am working with now uses base-graphics - but mostly I'll be using ggplot2 - which makes the issue somewhat more important (ggplot2 is excellent, but in the current implementation not the fastest))
Thanks
It's not a problem for ggplot2 or lattice graphics - you always have to explicitly print them when they are called in non-interactive settings (like from within a script).
Good practise for coding R means wrapping as much of your code as possible into functions. (See, e.g., Chapter 5 of the R Inferno, pdf.) If you place your plotting code inside a function, it need not be displayed when you source it. Compare the following.
File foo.r contains
plot(1:10)
When you call source('foo.r'), the plot is shown.
File bar.r contains
bar <- function() plot(1:20)
When you call source('bar.r'), the plot is not shown. You can display it at your convenience by typing bar() at the command prompt.
Perhaps this might be of some help...
"A package that provides a null graphics device; includes a vignette, "devNull", that documents how to create a new graphics device as an add on package. "
from http://developer.r-project.org/
It's not the best sounding solution, but If you might be running this script often like this, you could declare a boolean whether graphics are required (graphics_required=TRUE) and then enclose all your plot commands in if/then clauses based on your boolean, then just change the boolean to change the behaviour of the script

Resources