Expression in R - r

I want to use expression for my ylab= when plotting in R. How do I get characters, using expression using the \mathcal{} style font? I am running Ubuntu and I don't want to use ggplot.
To clarify: part of the ylab will contain mathcal characters and part of it will not (and will be a formula). That's why I want to use expression
It is this symbol which I want:
= \mathcal{E}

One possible solution is to use tikz, which is kind of awesome, here is a much less beautiful example than the one they generate in the help of function tikzDevide::tikz:
require(filehash)
require(tikzDevice)
tikz("sinhplot.tex", width = 8, height = 4,
standAlone = TRUE,
packages = c("\\usepackage{tikz}",
"\\usepackage[active,tightpage,psfixbb]{preview}",
"\\PreviewEnvironment{pgfpicture}",
"\\setlength\\PreviewBorder{0pt}",
"\\usepackage{amssymb}")
plot(sinh, col="steelblue", lwd=2, xlim=c(-3,3), xlab="$\\mathcal E= [-3 , 3]$")
grid()
dev.off()
tools::texi2dvi(sinhplot,pdf=T)
system(paste(getOption('pdfviewer'),'sinhplot.pdf'))
The result looks quite nice!
Another solution is to just give up having beautiful characters in the labels of your figures...

Related

R plot title involving a subscript and value of a variable

In R, the value of the variable MyVar should be evaluated and appear in the title of a simple R plot p.
MyVar<-0.23
The plot p is called using
plot(p, main = MyTitle)
My plot title needs a subscript and should look like this:
What is the correct statement for MyTitle= ?
I tried dozens of variations using paste, expression, substitute, and bquote. Nothing seems to work...
Any help is highly appreciated!
Thanks a bunch!
Mark
I don't believe you can paste an expression to a variable (paste/print/cat/bquote/etc). As a workaround, you could use the "Fbelow=" expression as the title and use mtext to insert the value of MyVar, e.g.
MyVar<-0.23
plot(mtcars[2:3], main = expression('F'[below]*'='))
mtext(text = MyVar, side = 3, adj = 0.625, padj = -1.75, cex = 1.5)
Obviously this isn't ideal, but unless someone else has a clever way of solving your issue this will at least give you a potential option

Control the gap of pie labels in R?

t = table(iris$Species)
pie(t, labels=rownames(t))
This draws a simple pie. I want that the labels are a little bit more away from the pie. I checked the par() docu but I think I don't understand it completly and I missed the option for that.
This question is explicite about R's own pie() and not related to any other extern R package.
I don't think you can really do this with the pie function. If you look at View(pie) you'll see that the labels are drawn using the text function. This means that they are not really axis labels, and that par has little effect on them. You could try to do stuff by using the arguments of the text function (i.e. pos = 2, offset = 1) but this will affect all labels in the exact same way and results in warnings. To me it seems that the only way is the stupid way by adding some spaces before/ after labels. ie:
t = table(iris$Species)
nms = rownames(t)
# spaces needed after the labels
nms[2] = paste0(nms[2], strrep(' ', 7))
# spaces needed before the labels
nms[c(1, 3)] = paste0(strrep(' ', 7), nms[c(1, 3)])
pie(t, labels = nms)
If you want to a better solution, you could rewrite the pie function to be a bit more flexible or use a different package.

R barplot with German encoding

My R script uses the R visialization library barplot. The problem is, that I need German characters such as äöü in the labeling.
I am working with the Eclipse plugiin StatET and on a 64-bit-Windows system. I tried to set up the correct encoding by Sys.setlocale(category="LC_ALL", locale="German_Germany");
Tests with print("äöü") gives the correct result, but when integrating those "Umlauts" in the barplot, it the graph shows the labeling with characters such as ä.
plot <- barplot(as.matrix(comp), beside=TRUE, ylim = c(0,100), main="äöü", legend.text = TRUE);
Any idea how to solve the problem?
EDIT
The result for Sys.getlocale('LC_CTYPE') is:
[1] "German_Germany.1252"
I can see the letters properly without alteration. Maybe try:
plot <- barplot(df, main= enc2utf8("äöü"), legend.text = TRUE);
As proposed here.
I do not know about Eclipse or 64-bit Windows, but since the question in the OP is phrased more generally: As far as I understand it, at least for exporting a plot as pdf, it is generally sufficient to set the correct locale (as you have) and putting the characters in octal representation (\344\366\374). E.g,
Sys.setlocale("LC_CTYPE", "german")
plot <- barplot(as.matrix(comp), beside=TRUE, ylim = c(0,100), main="\344\366\374\337", legend.text = TRUE)

Color option in xtsExtra

I am having trouble adjusting the colors of a multiple time series plot using xtsExtra.
This is the code of a minimal example:
require("xtsExtra")
n <- 50
data <- replicate(2, rnorm(n))
my.ts <- as.xts(ts(data, start=Sys.Date()-n, end=Sys.Date()))
plot.zoo(my.ts, col = c('blue', 'green'))
plot.xts(my.ts, col = c('blue', 'green'))
The plot.zoo commands yields
,
whereas the plot command from the xtsExtra package results in
.
In the second plot, the two time series are nicely overlaid, but seem insensitive to the col option.
I'm using the latest version 0.0-1 of the xtsExtra package (rev. 862).
It is my understanding that the xts and xtsExtra packages are designed as extensions of zoo and should work with the same arguments (plus many additional ones). Even though I can get the same overlay behavior in plot.zoo using the screens option, I cannot really resort to using it because the call to plot.xts that causes my problems is within the quantstrat package (functions chart.forward.training and chart.forward.testing for example) which I'd loathe to modify. (Incidentally, the dev.new() in these functions is causing me trouble as well.)
Question: Why does plot from the xtsExtra package seem not to respond to the col= option and what can be done about it, if modifying
the call to the function is not a real option?
Q1. If you take time to read the help text for plot.xts, you see that the function does not have a col argument. Together with the fact that partial matching of argument names doesn't seem to be allowed in the function, it explains why plot.xts it does not respond col =.
Compare with a case where partial matching works:
plot(x = 1:2, y = 1:2, type = "b"); plot(x = 1:2, y = 1:2, ty = "b"); "ty" matches "type".
See here: "If the name of the supplied argument matches exactly with the first part of a formal argument then the two arguments are considered to be matched".
Q2. Instead you may use the colorset argument:
"color palette to use, set by default to rational choices" (colorset = 1:12).
plot.xts(my.ts, colorset = c('blue', 'green'))

R legend pch mix of character and numeric

Is it possible to use a mix of character and number as plotting symbols in R legend?
plot(x=c(2,4,8),y=c(5,4,2),pch=16)
points(x=c(3,5),y=c(2,4),pch="+")
legend(7,4.5,pch=c("+",16),legend=c("A","B")) #This is the problem
Use the numerical equivalent of the "+" character:
plot(x=c(2,4,8),y=c(5,4,2),pch=16)
points(x=c(3,5),y=c(2,4),pch="+")
legend(7,4.5,pch=c(43,16),legend=c("A","B"))
There are actually numerical equivalents for all symbols!
Source: Dave Roberts
The pch code is the concatenation of the Y and X coordinates of the above plot.
For example, the + symbol is in row (Y) 4 and column (X) 3, and therefore can be drawn using pch = 43.
Example:
plot(x=c(2,4,8),y=c(5,4,2),pch=16)
points(x=c(3,5),y=c(2,4),pch="+")
legend(7,4.5,pch=c(43,16),legend=c("A","B"))
My first thought is to plot the legend twice, once to print the character symbols and once to print the numeric ones:
plot(x=c(2,4,8),y=c(5,4,2),pch=16)
points(x=c(3,5),y=c(2,4),pch="+")
legend(7,4.5,pch=c(NA,16),legend=c("A","B")) # NA means don't plot pt. character
legend(7,4.5,pch=c("+",NA),legend=c("A","B"))
NOTE: Oddly, this works in R's native graphical device (on Windows) and in pdf(), but not in bmp() or png() devices ...
I bumped to this issue several time, so I wrote a tiny function below. You can use to specify the pch value, e.g.
pch=c(15:17,s2n("|"))
String to Numeric
As noted in previous answers, you can simply add the numerical equivalent of the numeric and character symbols you want to plot.
However, just a related aside: if you want to plot larger numbers (e.g., > 100) or strings (e.g., 'ABC') as symbols, you need to use a totally different approach based on using text().
`Plot(x,y,dat,type='n') ; text(x,y,labels = c(100,'ABC')
Creating a legend in this case is more complicated, and the best approach I've ever come up with is to stack legends on top of each other and using the legend argument for both the pch symbol and the description:
pchs <- c(100,'ABC','540',sum(13+200),'SO77')
plot(1:5,1:5,type='n',xlim=c(1,5.1))
text(1:5,1:5,labels = pchs)
legend(3.5,3,legend = pchs,bty='n',title = '')
legend(3.5,3,legend = paste(strrep(' ',12),'ID#',pchs),bty='n',title='Legend')
rect(xleft = 3.7, ybottom = 1.5, xright = 5.1, ytop = 3)
This uses strrep to concatenate spaces in order to shift the text over from the "symbols", and it uses rect to retroactively fit a box around the printed legend text.

Resources