how to use expression in in names.arg function - r

I have several carbon compounds in my bar graph but I need to subscript the numbers associated with each one, e.g. C*12*-C*16*, C*17*-C*25*,C*26*-C*32*,C*33*-C*37*.
I am trying to use the expression function but it is not going well with the names.arg function on barplot. Please see the code so far:
barplot(t(as.matrix(alkane)),
ylim=c(0,100),
names.arg=c("C12-C16","C17-C25","C26-C32","C33-C37"),
xlab="Hydrocarbons analysed after 21 days",
ylab="% loss in concentration relative to day 0",
cex.lab=0.8,
cex.axis=0.8,
cex.names=0.8,
cex.main=0.8,
beside=TRUE,
col=c("blue","red"))
Any help would be really appreciated.

I suppose you are looking for this:
set.seed(1)
x <- matrix(rbeta(8, 5, 5), 2, 4)
barplot(x, names.arg =
expression(C[12]-C[16], C[17]-C[25], C[26]-C[32],C[33]-C[37]))

As you can see with the highlighting it is not properly formatted your names.arg, check the " to be paired:
names.arg=c("C12-C16","C17-C25","C26-C32","C33-C37"),

Related

Problem in Greek letter on R plots using Rqtl package

I am trying to plot a QTL graph with a greek symbole in my legend like this :
LOD(π,μ)
Here is my code :
plot(outFW.2p14a,lodcolumn=1:3, col = c("black","blue","green"),
ylab="LOD scores for 2014",ylim=c(0,5), main="B")
legend("topright",
legend=c("LOD(π,μ)", "LOD(π)","LOD(μ)"," ",
"LOD threshold (μ)","LOD threshold (π)","LOD threshold (π,μ)"),
col=c("black","blue","green","white","red","red","red"),
lty=c(1,1,1,1,2,3), cex=1.2)
When I use this code I get the right plot but the legend where the greek symbole π appears is wrong and show a 'p' instead...Just like this :
Legend example with error and for μ it works just fine !
I am using genetic data and tables from Rqtl package but I don't think that the problem comes from there but more from the plot function. Do you have any idea why μ works and not π ?
I would appreciate some help for this because I have tried many things and I can not get it right. I need it for a publication in a scientific journal so it needs to be right.
Best regards,
Diana
I got the symbol correctly if i use your code..
COLS=c("black","blue","green","white","red","red","red")
LABEL1 = c("LOD(π,μ)", "LOD(π)","LOD(μ)"," ",
"LOD threshold (μ)","LOD threshold (π)","LOD threshold (π,μ)")
plot(NULL,ylab="LOD scores for 2014",ylim=c(0,5), main="B",xlim=c(0,5))
legend("topright",
legend=LABEL1,
col=COLS,
lty=c(1,1,1,1,2,3), cex=1.2)
Might be something weird with your keyboard, try this, where you call the symbol through expression:
LABEL2 = c(expression(paste("LOD(",pi,",",mu,")")),
expression(paste("LOD(",pi,")")),
expression(paste("LOD(",mu,")")),
expression(paste("LOD threshold(",pi,",",mu,,")")),
expression(paste("LOD threshold (",pi,")")),
expression(paste("LOD threshold (",mu,")"))
)
plot(NULL,ylab="LOD scores for 2014",ylim=c(0,5), main="B",xlim=c(0,5))
legend("topright",
legend=LABEL2,
col=COLS,
lty=c(1,1,1,1,2,3), cex=1.2)

Boxplot graph getting skew

I'm really new in R statistics and i just bumped into a problem when it comes to creating a box plot. I have used the following script that i have created:
# Reset R's braingetwd
rm(list=ls())
# Tells R where to look
setwd("/Users/Axel/Desktop/Kandidatarbete/Data")
# Confirms R is looking at the right place
getwd()
# Read data
read.table("migration_test_graph.txt")
# Assign a name to the data
migrationtest5<- read.table("migration_test_graph.txt", ,col.names=c('treatment','migration'), header=TRUE, sep="\t", na.strings="NA", dec=",", strip.white=TRUE)
mzmz=migrationtest5[which(migrationtest5$treatment == "MzMz"),]
mzct=migrationtest5[which(migrationtest5$treatment == "MzCt"),]
ctct=migrationtest5[which(migrationtest5$treatment == "CtCt"),]
ctmz=migrationtest5[which(migrationtest5$treatment == "CtMz"),]
#plot data (migration in function of index)
boxplot(migration~treatment, data=migrationtest5, boxwex=0.5, varwidth=TRUE, staplewex=FALSE, outline=TRUE, outpch=20, cex= 0.3, range=1, title=FALSE, whisklty=19, col=c("darkolivegreen","darkolivegreen","darkolivegreen3", "darkolivegreen3"), xlab="", ylab="Migration index", las=1, ylim=c(0, 1), cex.lab=1, cex.axis=0.75, axes=FALSE, border=NA, medcol="white", whiskcol="black", outcol="black", whisklwd=2, medlwd=4)
axis(1, at=seq(0, 5, by=1), cex.axis=1, labels=c("", "CtCt","MzCt", "CtMz", "MzMz",""), lwd=2)
axis(2, at=seq(0,1, by=0.2), las=2, cex.axis=1, lwd=2)
The problem is that the whole graph looks skew and the data are located on the wrong places. Anyone got a clue what might be the problem? The big box plots are supposed to be located above the CtMz and MzMz and the small ones above the MzCt and CtCt.
Thanks in advance
Axel

(R) Axis widths in gbm.plot

Hoping for some pointers or some experiences insight as i'm literally losing my mind over this, been trying for 2 full days to set up the right values to have a function spit out clean simple line plots from the gbm.plot function (packages dismo & gbm).
Here's where I start. bty=n in par to turn off the box & leave me with only left & bottom axes. Gbm.plot typically spits out one plot per explanatory variable, so usually 6 plots etc, but I'm tweaking it to do one per variable & looping it. I've removed the loop & lots of other code so it's easy to see what's going on.
png(filename = "whatever.png",width=4*480, height=4*480, units="px", pointsize=80, bg="white", res = NA, family="", type="cairo-png")
par(mar=c(2.6,2,0.4,0.5), fig=c(0,1,0.1,1), las=1, bty="n", mgp=c(1.6,0.5,0))
gbm.plot(my_gbm_model,
n.plots=1,
plot.layout = c(1,1),
y.label = "",
write.title=F,
variable.no = 1, #this is part of the multiple plots thing, calls the explanatory variable
lwd=8, #this controls the width of the main result line ONLY
rug=F)
dev.off()
So this is what the starting condition looks like. Aim: make the axes & ticks thicker. That's it.
Putting "lwd=20" in par does nothing.
Adding axes=F into gbm.plot() turns the axes and their numbers off. So I conclude that the control of these axes is handled by gbm.plot, not par. Here's where it get's frustrating and crap. Accepted wisdom from searches says that lwd should control this but it only controls the wiggly centre line as per my note above. So maybe I could add axis(side=1, lwd=8) into gbm.plot() ?
It runs but inexplicably adds a smoother! (which is very thin & hard to see on the web but it's there, I promise). It adds these warnings:
In if (smooth & is.vector(predictors[[j]])) { ... :
the condition has length > 1 and only the first element will be used
Fine, R's going to be a dick for seemingly no reason, I'll keep plugging the leaks as they come up. New code with axis as before and now smoother turned off:
png(filename = "whatever.png",width=4*480, height=4*480, units="px", pointsize=80, bg="white", res = NA, family="", type="cairo-png")
par(mar=c(2.6,2,0.4,0.5), fig=c(0,1,0.1,1), las=1, bty="n", mgp=c(1.6,0.5,0))
gbm.plot(my_gbm_model,
n.plots=1,
plot.layout = c(1,1),
y.label = "",
write.title=F,
variable.no = 1,
lwd=8,
rug=F,
smooth=F,
axis(side=1,lwd=8))
dev.off()
Gives error:
Error in axis(side = 1, lwd = 8) : plot.new has not been called yet
So it's CLEARLY drawing axes within plot since I can't affect the axes from par and I can turn them off in plot. I can do what I want and make one axis bold, but that results in a smoother and warnings. I can turn the smoother off, but then it fails because it says plot.new hadn't been called. And this doesn't even account for the other axis I have to deal with, which also causes the plot.new failure if I call 2 axis sequentially and allow the smoother.
Am I the butt of a big joke here, or am I missing something obvious? It took me long enough to work out that par is supposed to be before all plots unless you're outputting them with png etc in which case it has to be between png & plot - unbelievably this info isn't in ?par. I know I'm going off topic by ranting, sorry, but yeah, 2 full days. Has this been everyone's experience of plotting in R?
I'm going to open the vodka in the freezer. I appreciate I've not put the full reproducible code here, apologies, I can do if absolutely necessary, but it's such a huge timesuck to get to reproducible stage and I'm hoping someone can see a basic logical/coding failure screaming out at them from what I've given.
Thanks guys.
EDIT: reproducibility
core data csv: https://drive.google.com/file/d/0B6LsdZetdypkWnBJVDJ5U3l4UFU
(I've tried to make these data reproducible before and I can't work out how to do so)
samples<-read.csv("data.csv", header = TRUE, row.names=NULL)
my_gbm_model<-gbm.step(data=samples, gbm.x=1:6, gbm.y=7, family = "bernoulli", tree.complexity = 2, learning.rate = 0.01, bag.fraction = 0.5))
Here's what will widen your axis ticks:
..... , lwd.ticks=4 , ...
I predict on the basis of no testing because I keep getting errors with what limited code you have provided) that it will get handled correctly in either gbm.plot or in a subsequent axis call. There will need to be a subsequent axis call, two of them in fact (because as you noted 'lwd' gets passed around indiscriminately):
png(filename = "whatever.png",width=4*480, height=4*480, units="px", pointsize=80, bg="white", res = NA, family="", type="cairo-png")
par(mar=c(2.6,2,0.4,0.5), fig=c(0,1,0.1,1), las=1, bty="n", mgp=c(1.6,0.5,0))
gbm.plot(my_gbm_model,
n.plots=1,
plot.layout = c(1,1),
y.label = "",
write.title=F,
variable.no = 1,
lwd=8,
rug=F,
smooth=F, axes="F",
axis(side=1,lwd=8))
axis(1, lwd.ticks=4, lwd=4)
# the only way to prevent `lwd` from also affecting plot line
axis(2, lwd.ticks=4, lwd=4)
dev.off()
This is what I see with a simple example:
png(); Speed <- cars$speed
Distance <- cars$dist
plot(Speed, Distance,
panel.first = lines(stats::lowess(Speed, Distance), lty = "dashed"),
pch = 0, cex = 1.2, col = "blue", axes=FALSE)
axis(1, lwd.ticks=4, lwd=4)
axis(2, lwd.ticks=4, lwd=4)
dev.off()

Heatmap like plot with Lattice

I can not figure out how the lattice levelplot works. I have played with this now for some time, but could not find reasonable solution.
Sample data:
Data <- data.frame(x=seq(0,20,1),y=runif(21,0,1))
Data.mat <- data.matrix(Data)
Plot with levelplot:
rgb.palette <- colorRampPalette(c("darkgreen","yellow", "red"), space = "rgb")
levelplot(Data.mat, main="", xlab="Time", ylab="", col.regions=rgb.palette(100),
cuts=100, at=seq(0,1,0.1), ylim=c(0,2), scales=list(y=list(at=NULL)))
This is the outcome:
Since, I do not understand how this levelplot really works, I can not make it work. What I would like to have is the colour strips to fill the whole window of the corresponding x (Time).
Alternative solution with other method.
Basically, I'm trying here to plot the increasing risk over time, where the red is the highest risk = 1. I would like to visualize the sequence of possible increase or clustering risk over time.
From ?levelplot we're told that if the first argument is a matrix then "'x' provides the
'z' vector described above, while its rows and columns are
interpreted as the 'x' and 'y' vectors respectively.", so
> m = Data.mat[, 2, drop=FALSE]
> dim(m)
[1] 21 1
> levelplot(m)
plots a levelplot with 21 columns and 1 row, where the levels are determined by the values in m. The formula interface might look like
> df <- data.frame(x=1, y=1:21, z=runif(21))
> levelplot(z ~ y + x, df)
(these approaches do not quite result in the same image).
Unfortunately I don't know much about lattice, but I noted your "Alternative solution with other method", so may I suggest another possibility:
library(plotrix)
color2D.matplot(t(Data[ , 2]), show.legend = TRUE, extremes = c("yellow", "red"))
Heaps of things to do to make it prettier. Still, a start. Of course it is important to consider the breaks in your time variable. In this very simple attempt, regular intervals are implicitly assumed, which happens to be the case in your example.
Update
Following the advice in the 'Details' section in ?color2D.matplot: "The user will have to adjust the plot device dimensions to get regular squares or hexagons, especially when the matrix is not square". Well, well, quite ugly solution.
par(mar = c(5.1, 4.1, 0, 2.1))
windows(width = 10, height = 2.5)
color2D.matplot(t(Data[ , 2]),
show.legend = TRUE,
axes = TRUE,
xlab = "",
ylab = "",
extremes = c("yellow", "red"))

Creating a superscript in mtext

I have the following dataframe
Data1 <- data.frame(pH = c(8,8.5,6,7.1,9), EC50 = c(20,11,5,25,50))
Data2 <- data.frame(pH = c(7,7.2,6.5,8.2,8.5), EC50 = c(13,15,18,25,19))
Using par I create two graphs on one plot:
par(mfrow=c(2,1), oma=c(3,3,1,1), mar=c(2,2,3,1), cex.axis=1.3)
plot(x=Data1[,'pH'], y=Data1[,'EC50'])
plot(x=Data2[,'pH'], y=Data2[,'EC50'])
Because I used par, I cannot specify the xlab and ylab in plot,
therefore I use mtext.
I would like to write a superscript in my ylab, however, I do not know how to do so,
when using mtext.
I have tried the following
mtext(expression("Cu^{2+} at EC50"), side=2, line = 4, padj=1, at=30, cex=1.2)
but can't seem to get the 2+ as a superscript above the Cu.
Any help is more than welcome!
mtext(expression(paste("Cu"^"2+","at EC50",sep="")))
You have to build your expression within the expression call. See the examples at Mathematical Annotation in R.
In your case I found
mtext(expression(paste( plain("Cu") ^ plain("2+"), plain(" at EC50") )), side=2, line = 4, padj=1, at=30, cex=1.2)
gave a reasonable result

Resources