R quantmod chart_Series: using large fonts for y axis - r

I am trying to use a larger font size for those who have poor eyesight.
library(quantmod)
getSymbols("SPY", from="2013-11-01", to=Sys.Date())
chart_Series(SPY)
myPars <-chart_pars()
myPars$cex<-1.5
chart1 <- chart_Series(SPY, pars=myPars)
chart1
However, when I do this, only a part of the y axis numbers scale are shown.
Is it possible to shift the chart, so the y axis numbers scale are not cut off.
Thank you for your help.

When I try your code (Note this is in R 3.1.0 in R studio though), the y axis numbers scale doesn't appear to be cut off. Nevertheless, you can adjust the chart_pars() (as you've already partly done) and chart_theme() to achieve what you want.
To reduce crowding on the y-axis, you can adjust the margin parameters, $mar, of chart_pars(). Increase the left (and or right) margin parameter values to remove crowding of the y-axis numbers. You can also consider removing either the left or right y-axis scale to save more space. Here's an example, with explanations:
library(quantmod)
getSymbols("SPY", from="2013-11-01", to=Sys.Date())
myPars <- chart_pars()
myPars$mar <- c(3, 2, 0, .2) # default is c(3, 1, 0, 1) # bottom, left, top, right
myPars$cex <- 1.5 #' Increase font size of both x and y axis scale ticks
mychartTheme <- chart_theme()
mychartTheme$rylab = FALSE #' Don't show y-axis on right side of plot to save space
# mychartTheme$lylab = TRUE #' Show y-axis ticks on left side of plot? Default is TRUE for both left and right sides.
chart1 <- chart_Series(SPY, pars=myPars, theme = mychartTheme)
chart1
The plot you'll get from this code is:
Furthermore, in case you're interested in editing the number of decimal places displayed on the y-axis scale numbers (e.g. in FX for currencies quoted in pips at 1e-4), you can edit the source code of chart_Series at certain lines to get what you desire.
For example, to plot to 4 decimal places on the LEFT y-axis only, and offset the printing of the y-axis numbers to the left (so they don't plot under the bars near the left margin of the plot), you could edit lines 143-147 of chart_Series like so (create a copy of chart_Series with the following edits):
#' my.chart_Series is identical to the definition of chart_Series, with these minor edits
my.chart_Series <- function (x, name = deparse(substitute(x)), type = "candlesticks",
subset = "", TA = "", pars = chart_pars(), theme = chart_theme(),
clev = 0, ...)
{
cs <- new.replot()
....
[lines 143-147]: if (theme$lylab) {
cs$add(expression(text(1 - 1/3 - max(strwidth(alabels)),
alabels, sprintf("%.4f", alabels), #alabels, noquote(format(alabels, justify = "left", digits = 4)),
col = theme$labels, offset = -2, cex = 0.9, pos = 4,
xpd = TRUE)), expr = TRUE)
} #' default offset = 0
....
}
And then in your R script to see this effect write something like this:
source('my.chart_Series.R')
environment(my.chart_Series) <- environment(get("chart_Series", envir = asNamespace("quantmod")))
assignInNamespace(x = "chart_Series", value = my.chart_Series, ns = "quantmod")
myPars <- chart_pars()
myPars$mar <- c(3, 3, 0, .2) # default is c(3, 1, 0, 1) # bottom, left, top, right
myPars$cex <- 1.0 #' Increase font size of both x and y axis scale ticks
mychartTheme <- chart_theme()
mychartTheme$rylab = FALSE #' Don't show y-axis on right side of plot to save space
# mychartTheme$lylab = TRUE #' Show y-axis ticks on left side of plot? Default is TRUE for both left and right sides.
chart1 <- quantmod:::chart_Series(SPY, pars=myPars, theme = mychartTheme) #' Note the need to prepend the quantmod namespace to the modified visible chart_Series function in quantmod.
chart1
This will give you this plot:
Also, to reduce the number of ticks drawn on the y axis, you can also modify line 128 in chart_Series like so:
p <- pretty(ylim, n = 5) #' The original source code (quantmod 0.4-0) is p <- pretty(ylim, 10)
See the R help documentation for constraints on the n argument in the R function pretty. This gives:

Related

Missing right tick marks in R latticeExtra c.trellis

When using latticeExtra:::c.trellis to combine plots, the right-side tick marks and text/numeric labels go missing, and I'd like to bring them back:
library(latticeExtra)
set.seed(1)
foo <- data.frame(x = 1:100,
y = 1:100 + rnorm(100))
foo$resid <- with(foo, x-y)
## Plot 1 -----
(p1 <- xyplot(y~x, foo))
## Plot 2 -----
(p2 <-
xyplot(resid~x, foo,
scales = list(rot = 0, tck = c(1,1), alternating = 3),
between = list(y = 1), ylab.right = "ylab.right",
# par.settings = list(axis.components =
# list(right = list(pad1 = 2, pad2 = 2)))
# Note: this padding attempt does not restore the missing ticks,
# pad arguments get ignored when using c.trellis below
))
# tick marks appear on all four sides (as desired)
## Combine -----
(p12 <- latticeExtra:::c.trellis(p2, p1,layout = c(1,2)))
# right tick marks are missing
Is there a way to restore the right-side ticks and/or labels manually, say, by modifying the combined trellis object?
From the help file ?c.trellis:
Description
Combine the panels of multiple trellis objects into one.
and later,
Note that combining panels from different types of plots does not really fit the trellis model. Some features of the plot may not work as expected. In particular, some work may be needed to show or hide scales on selected panels. An example is given below.
It looks to me that you really aren't trying to combine panels into one object. You even use between to put some separation. Rather, you are trying to combine two plots.
You can use print,
print(p1,split=c(1,1,1,2),more=TRUE)
print(p2,split=c(1,2,1,2),more=FALSE)
See ?print.trellis.

Dot plots (as opposed to dotplots) in R

I am trying to write a function that will produce what I regard as a real dot plot (unlike the Cleveland variety, I require a univariate scatterplot with the dots stacked for (nearly) equal values). I have come close:
In this illustration, the dots you see are actually rotated text strings of lower-case "o"s. It is done this way because I need the dot spacing to stay constant if the plot is re-scaled. However, I'd like something better than lower-case "o"s, for example, filled dots instead of circles. This could be done if I could access the font that is used for the standard plotting symbols (pch = 1:25 in the plot function and relatives). Then I could make a text string with that font and get what's needed. Does anybody know how to do that?
PS - No, a histogram with lots of bins is not an acceptable substitute.
I did find a way to get the desired dot plot using low-level graphics parameters (namely "usr", the actual user coordinates of the plotting area, and "cxy", the character size). The recordGraphics() function wraps the part that needs to be changed when the graph is resized. Here's the function:
dot.plot = function(x, pch = 16, bins = 50, spacing = 1, xlab, ...) {
if(missing(xlab))
xlab = as.character(substitute(x))
# determine dot positions
inc = diff(pretty(x, n = bins)[1:2])
freq = table(inc * round(x / inc, 0))
xx = rep(as.numeric(names(freq)), freq)
yy = unlist(lapply(freq, seq_len))
# make the order of the dots the same as the order of the data
idx = seq_along(x)
idx[order(x)] = idx
xx = xx[idx]
yy = yy[idx]
# make a blank plot
plot(xx, yy, type = "n", axes = FALSE, xlab = xlab, ylab = "")
# draw scale
axis(1)
ylow = par("usr")[3]
abline(h = ylow) # extend to full width
# draw points and support resizing
recordGraphics({
yinc = 0.5 * spacing * par("cxy")[2]
points(xx, ylow + yinc * (yy - .5), pch = pch, ...)
},
list(),
environment(NULL))
invisible()
}
The spacing argument may be used if you want a tighter or looser gap between dots. An example...
with(iris, dot.plot(Sepal.Length, col = as.numeric(Species)))
This is a better solution than trying to do it with text, but also a little bit scary because of the warnings you see in the documentation for recordGraphics

Rgraphviz: edge labels outside plotting region

I am trying to plot a Rgraphviz object with two edge labels. Unfortunately the labels fall outside the plot. Here is my example:
require('Rgraphviz')
set.seed(123)
g1 <- randomGraph(letters[1:10], 1:4, 0.4)
eAttrs <- list()
eAttrs$label <- c("a~g" = "I have a very long label 1", "a~i" = "and a long label 2")
plot(g1, edgeAttrs = eAttrs)
Here is my plot:
I tried several things with no success:
1.
Set a larger bounding box
z <- agopen(g1, "foo")
z#boundBox#upRight#x <- z#boundBox#upRight#x + 300
z#boundBox#upRight#y <- z#boundBox#upRight#y + 300
plot(z, edgeAttrs = eAttrs)
2.
Decrease the label fontsize (not really what I want in my application, anyways)
eAttrs$labelfontsize=c("a~g"="3")
plot(g1, edgeAttrs = eAttrs)
3.
Change par attributes:
par(oma=c(10,10,10,10))
plot(g1, edgeAttrs = eAttrs)
4.
Change node, edge and general attributes from ?Rgraphviz::GraphvizAttributes
attrs <- list(graph=list(size=c(1, 1)))
attrs$edge$fontsize<-8
plot(g1, edgeAttrs = eAttrs, attrs=attrs)
None of my attempts seem to work. Does anyone have an idea?
The problem
Calling plot() on a graph object dispatches an S4 method (shown by getMethod("plot", "graph")), which in turn calls the function shown by typing getMethod("plot", "Ragraph"). That function contains the following rather unfortunate lines which, regardless of any related parameter settings you've made, will override them to reset the left and right margins to 0. Frustrating!
oldpars <- par(mai = c(sheight, 0, mheight, 0))
on.exit(par(oldpars), add = TRUE)
A workaround
One workaround is to construct a three panel layout in which the left and right panels are just there to provide a bit of buffering space. Turn off clipping, plot your graph object in the middle panel, and it then seems to work:
layout(matrix(1:3, nrow=1), widths=c(1,5,1))
par(xpd=NA) ## turn off all clipping
plot.new() ## blank plot in Panel 1
plot(g1, edgeAttrs = eAttrs) ## graph in Panel 2
plot.new() ## blank plot in Panel 3
I found another solution: In my original question I changed the size of the bounding box in a laid out graph I got with agopen. Plotting the laid out graph showed no edge labels at all. I found that it is possible to pass the edge attributes from the graph object to agopen and then change the bounding box:
require('Rgraphviz')
set.seed(123)
g1 <- randomGraph(letters[1:10], 1:4, 0.4)
eAttrs <- list()
eAttrs$label <- c("a~g" = "I have a very long label 1", "a~i" = "and a long label 2")
z <- agopen(g1, "foo", edgeAttr=eAttrs)
z#boundBox#botLeft#x <- z#boundBox#botLeft#x - 400 ##left
z#boundBox#upRight#x <- z#boundBox#upRight#x + 200 ##right
plot(z)
The plot:

aligning patterns across panels with gridExtra and grid.pattern()

The gridExtra package adds a grob of class "pattern" that lets one fill rectangles with patterns. For example,
library(gridExtra)
grid.pattern(pattern = 1)
creates a box filled with diagonal lines. I want to create a stack of panels in which each panel is filled with these diagonal lines. This is easy:
library(lattice); library(gridExtra)
examplePlot <- xyplot(
1 ~ 1 | 1:2,
panel = function () grid.pattern(pattern = 1),
layout = c(1, 2),
# Remove distracting visual detail
scales = list(x=list(draw=FALSE), y=list(draw=FALSE)),
strip = FALSE, xlab = '', ylab = ''
)
print(examplePlot)
The problem is that the diagonal lines aren't aligned across panels. That is, there is a visual "break" where the bottom of the first panel meets the top of the second panel: at that point, the lines don't line up. This is the problem that I want to fix.
I can eliminate most of the visual break by adding the argument pattern.offset = c(.2005, 0) to the grid.pattern call, and making sure that it applies only to the bottom panel. But this solution doesn't generalize. For example, if I change the pattern (e.g., by using the granularity argument to grid.pattern), this solution won't work. Is there a more general fix?
To make this work, you'll have to take charge of setting the panel.height argument used by print.trellis. (To see why, try resizing your plotting device after running your example code: as the size of the device and the panels changes, so does the matching/mismatching of the lines):
## Calculate vertical distance (in mm) between 45 degree diagonal lines
## spaced 5mm apart (the default distance for grid.pattern).
vdist <- 5 * sqrt(2)
nLines <- 8L ## can be any integer
panelHeight <- list(x = nLines*vdist, units = "mm", data = NULL)
## Plot it
print(examplePlot, panel.height=panelHeight)

R par(omd) does not contain within mfrow

I would like two plots to show up in two seperate spaces in the plot so I do:
par(mfrow=c(1,2))
plot(1:10,1:10)
Now I would like the second plot to be about 25% shorter than the first plot so I adjust omd:
tmp <- par()$omd
tmp[4] <- 0.75
par(omd=tmp)
plot(1:10,1:10)
The problem is that the second plot shows up ontop of the first plot. How do I avoid this margin issue?
Maybe try using layout instead?
layout(matrix(c(1, 1, 0, 2), ncol = 2L), widths = c(1,1),heights = c(0.5,1))
par(mar = c(3,2,2,2))
plot(1:10,1:10)
par(mar = c(3,2,2,2))
plot(1:10,1:10)
I guess maybe you'd want to set the heights to c(0.2,0.8) to get a 25% reduction?
Edit
But I don't think that omd does what you think it does. It changes the region inside the outer margins, which will always include both plot regions when setting par(mfrow = c(1,2)). What you really want to change, I think is plt, which alters the size of the current plotting region (using quartz, as I'm on a mac):
quartz(width = 5,height = 5)
par(mfrow=c(1,2))
vec <- par("plt")
plot(1:10,1:10)
par(plt = vec * c(1,1,1,0.75))
plot(1:5,1:5)

Resources