R barplot with German encoding - r

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)

Related

How do I change line thickness in denscomp plots from the fitdistrplus package in R?

I'm over-plotting three densities onto my data histogram, using denscomp in the fitdistrplus package in R. The code below is working perfectly, but I don't know how to make the lines thicker.
denscomp(list(TryWeibull, TryGamma, TryLognormal), legendtext = plot.legend,
fitcol = c("indianred3","gray38", "darkblue"), fitlty = c("dashed", "longdash", "dotdash"),
xlab = "Age", ylab = "Proportion", main="")
fitcol is giving me the correct colours, fitly is giving me the correct line types, but I can't work out the command to make the lines thicker. I have two distribution densities that are close together and I have been unsuccessful in clearly identifying them using colour/line type differences. .
I am trying to de-emphasize the Weibull and emphasise the gamma and lognormal. The proportions are estimates, so I am trying to fit the general shape, not the exact values.
I can't see an option in the denscomp function to specify line widths. I would rather not use the ggplot option, but can shift to that if required. I was hoping there was a function option I'm overlooking.
Edited to add: I raised this as a feature request on GitHub and it has been implemented into the package.
Although the author of this package allows you to specify multiple line types (fitlty) and line colours (fitcol), they didn't allow you to specify multiple line widths. But since R is open-source, you are free to modify the function in any way.
Type the following at the R console:
fix(denscomp)
Then add a new argument to the function after fitcol, called fitlwd.
..., fitcol, fitlwd, addlegend = TRUE, ...
Then after line 30 add the following:
if (missing(fitlwd))
fitlwd <- 1
Then after line 34 add the following:
fitlwd <- rep(fitlwd, length.out = nft)
Then modify line 136 as follows:
col = fitcol[i], lwd=fitlwd[i], ...)
Finally, modify line 142:
col = fitcol, lwd=fitlwd,
Save and call the new function as before but now specifying the fitlwd argument:
denscomp(..., fitlwd=c(1,3,3))
I had the same question and followed Edward's solution, which was great and I learnt a lot, but it turned out you can just use ggplot to do that.
denscomp(..., plotstyle = "ggplot") + geom_line(linetype = "dashed",size = 1))

Expression in 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...

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'))

Inconsistent results saving png() and jpeg() in R

I am saving some complicated graphs off in an R program that include plot(), lines(), points() and abline() function calls and have tried using both png() and jpeg(), but both are rendering very inconsistent results. In one run the grid will be saved in the background, in the next it will not. In one run, the points will be added at the correct lwd, in another they will be huge, or maybe not added at all. In another run, a line will added, and then disappear when I run it again. I am looping through hundreds of iterations, and getting different results with almost every run.
png(paste("/someFilePlace/pics/", propIn, ".png", sep = ""))
plot(plotDat$yhat, col = "white", ylim = c(0,max(plotDat$yhat)*1.1),xaxt='n')
fairlylightgray <- rgb(204/255, 204/255, 204/255, alpha=0.4)
abline(v=(seq(0,1700,100)), col=fairlylightgray, lty="dotted")
abline(h=(seq(0,10,0.5)), col=fairlylightgray, lty="dotted")
points(plotDat$y, cex = '*', lwd = 3, col= "gray")
lines(plotDat$yhat, col = "#08519C")
axis(1, at = c(1,500,1000,1500),
labels = c(plotDat$dt[1],plotDat$dt[500],plotDat$dt[1000],plotDat$dt[1500]))
dev.off()
Congratulations, I think you may have found an obscure almost-bug (at least, failure to intercept a user error). Try replacing cex="*" with something sensible in your code (it should be a number -- or maybe you meant pch="*").
I am able to get different results in different PNGs as follows (if I plot in an X11 window I can get funny things to happen as I resize the window).
for (i in 1:10) {
png(paste("tmp",i,"png", sep="."))
plot(1:10,1:10,cex="*");
dev.off()
}

label of log y-axis: 1000 instead of 1e+03?

I've a problem concerning construction of log y-axis in a graphic.
How can I manage that the units/numbers of my log y-axis aren't shown in
1e+03, 1e+04, 1e+05 etc....
But only in regular Arabic numbers (1000, 10000, 100000)?
You need to remove the axis (by setting yaxt = "n") and then re-format it properly:
plot((1:100)^3, log = "y", yaxt = "n")
axis(2, format(c(1,10,100)^3, scientific=FALSE))
This was asked before on R-help.
Additionally, if you just don't like the look of 1e+03 scientific notation, the sfsmisc package has the axTexpr() function to format axis labels in a * 10^k notation.
library(sfsmisc)
example(axTexpr)
As I understand the question, the original poster wanted to get rid of the scientific notation of the labels. I had the same problem and found out this one works for that purpose (without using package sfsmisc from Kevin's answer, which I did not try):
plot((1:100)^3, log = "y", yaxt = "n")
axis(2, at=axTicks(2,log=TRUE), labels=format(axTicks(2, log=TRUE), scientific=FALSE))
This is rather late, but I was searching for the same solution. What I did (by searching, trial and error) is:
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
majorFormatter = FormatStrFormatter('%d') # shows 1 instead of 10^0 etc
and later, in the plot creating process:
ax = subplot(111) # ie one plot, but need to refer to it as 'ax'
semilogy(x,y)
and just before show(),
ax.yaxis.set_major_formatter(majorFormatter)
There may be unnecessary bits here, as I'm a rank Python newbie.

Resources