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

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.

Related

Remove axis labels from a plot in R after the plot has already been created

I am using the plot function of a particular package, namely the SPEI library. This function does not appear to accept any parameters to change the way the plot looks when it is generated.
I would like to know how to remove axis values, add new ones, and (ideally) rename the x-axis after the plot has already been created.
Please note that I have seen the other similar topics (e.g: Remove plot axis values) and they are not applicable to my situation. I know that when calling the base plot functions in R, you can set xaxt = "n", axes= FALSE, etc.
Here is a quick version of what I mean:
library(SPEI)
data(wichita)
x <- spei(wichita[,'PRCP'], 1)
plot.spei(x, main = "Here's a plot")
plot.spei(x, main = "Also a plot", xaxt = "n") #Note that xaxt does not affect output
That function uses base graphics and does not allow for any parameter to be passed through the function. There is no way to remove the x-axis labels without editing the function. Here's a way to make a copy and change just the one line that needs to be edited. (Note, since this method uses line numbers it's pretty fragile, this was tested with SPEI_1.7)
my_plot_spei <- plot.spei
my_plot_spei_body <- as.list(body(my_plot_spei))
my_plot_spei_body[[c(14,4,5)]] <- quote(plot(datt, type = "n", xlab = "", ylab = label, main = main[i], ...))
body(my_plot_spei) <- as.call(my_plot_spei_body)
then this will work
x <- spei(wichita[,'PRCP'], 1)
my_plot_spei(x, main = "Here's a plot", xaxt="n")

Changing y label plyr R studio

pretty new to R studio and R in general, I have a 3d scattered plot using the library scatterplot3d
The snip of code I have as follows:
s3d <- scatterplot3d(averageTime$Score, averageTime$Status, averageTime$Time, pch=16, highlight.3d=TRUE, type="h", main="3D Scatterplot", ylab="status", xlab= "Score", zlab="Time")
Right now the y axis comes out as 0-5 but I want to fill in custom text to replace it so as opposed to 0 I want the text "NA", 1 I would want "Covered"ect
Ive already tried applying the previous question seen here but to no avail: R: Replace X-axis with own values
please let me know if there is an easier application. Thank you
the answer is:
scatterplot3d(1:10, 1:10, 1:10, y.ticklabs=c("NA", "Covered", "Blah", "blub", "hmpf", "etc."))

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)

R axis text no dots

I want to add the following x-axis label to my bar plot but unfortunately R does not recognize the character '!' and prints dots instead of whitespaces:
I want: I get:
!src x.x.x.x X.src.x.x.x.x
!TCP X.TCP
!udp && !src x.x.x.x X.udp.....src.x.x.x.x
Additionally a would like to increase the margin because the text is to long and when setting the size over 'cex.names=0.6' then it just vanishes!?
There are two reason I can think of that R will have substituted X. for instances of !.
I suspect that the labelling you are seeing is due to R's reading of your data. Those column names aren't really syntactically valid and the erroneous character has been replaced by X.. This happens at the data import stage, so I presume you didn't check how R had read your data in?, or
You have a vector and the names of that vector are similarly invalid and R has done the conversion.
However, as you haven't made this reproducible it could be anything.
To deal with case 1 above, either edit your data file to contain valid names or pass check.names = FALSE in your read.table() call used to read in the data. Although doing the latter will make it difficult for you to select variable by name without quoting the name fully.
If you have a vector, then you can reset the names again:
> vec <- 1:5
> names(vec) <- paste0("!",LETTERS[1:5])
> vec
!A !B !C !D !E
1 2 3 4 5
> barplot(vec)
Also note that barplot() has a names.arg argument that you can use to pass it the labels to draw beneath each bar. For example:
> barplot(vec, names.arg = paste0("!", letters[1:5]))
which means you don't need to rely on what R has read in/converted for you as you tell it exactly what to label the plot with.
To increase the size of the margin, there are several ways to specify the size but I find setting it in terms of number of lines most useful. You change this via graphical parameter mar, which has the defaults c(5,4,4,2) + 0.1 which correspond to the bottom, left, top, and right margins respectively. Use par() to change the defaults, for example in the code below the defaults are store in op and a much larger bottom margin specified
op <- par(mar = c(10,4,4,2) + 0.1)
barplot(vec, names.arg = paste0("!", letters[1:5]), las = 2)
par(op) ## reset
The las = 2 will rotate the bar labels 90 degrees to be perpendicular to the axis.
One option is to use ann=F and add anotation to the plot using mtext.
x <- 1:2
y <- runif(2, 0, 100)
par(mar=c(4, 4, 2, 4))
plot(x, y, type="l", xlim=c(0.5, 2.5), ylim=c(-10, 110),
axes=TRUE, ann=FALSE)
Then add annotation:
mtext("!udp && !src x.x.x.x ", side=1, line=2)
Edit It is a question of a barplot and not simple plot.
as said in Gavin solution, the names argument can be setted. Here I show an example.
barplot(VADeaths[1:2,], angle = c(45, 135),
density = 20, col = "grey",
names=c("!src x.x.x.x", "!TCP", "!udp && !src x.x.x.x", "UF"),
horiz=FALSE)

Write x̄ (meaning average) in legend and how to prevent linebreak?

Good day!
I am not that familiar to R so I'd be glad to get a little help.
Assume I have the following minimal example:
test <- c(10,20,40,80,80)
avg <- mean(test)
avg <- format(avg,digits=2)
plot(test, xlab="x", ylab="y", pch = 4)
legend("topleft", legend= c("Average: ", avg))
I'd like to write x̄ instead of "average" - wonder if this is event possible as it's not a regular symbol - merely a combination of two (letter plus overline).
The other thing I'd like to get rid of is the line break after the word "Average (see arrow in graphic below):
There are two issues here. The first is that this is handled using ?plotmath in R. The operator you are looking for is bar(). This is not a function but markup that plotmath understands.
The second is that you need an expression in which avg is converted to its value. You need an expression because that is what plotmath works with. There are several solutions to this problem, but the one I use below is bquote(). You provide it an expression and anything wrapped in .( ) will be converted its value by evaluating the thing inside the .( ).
Here is your code and a suitably modified legend() call:
test <- c(10,20,40,80,80)
avg <- mean(test)
avg <- format(avg,digits=2)
plot(test, xlab="x", ylab="y", pch = 4)
legend("topleft", legend = bquote(bar(x)*":" ~ .(avg)))
Do note that this will insert exactly what is in avg. You may need to do
avg <- round(avg)
or some other formatting fix to get something nice and presentable.

Resources