Change xaxis label to specific letters R - r

Im making a lot of plots in R and a part of my code looks like this:
plot(x=0,y=0, type="n", ylim=c(0,250), xlim=c(0,8), bty="n", main = "Line 20 male 3 sec rep 2",
xlab = "Concentration", ylab = "MM above buttom")
fc <- levels(dat20m2$Conc)
for(i in 1:length(fc)){
tmp <- dat20m2[dat20m2$Conc==fc[i],]
points(y=tmp$t30.sum,x=rep(i,length(tmp$t30)))
points(y=mean(tmp$t30.sum),x=i,col="red")
}
abline(lm(t30.sum~as.integer(Conc),data=dat20m2), lty=2)
My x axis is from 0:8 but what i would like it to be is A, B, C, D, E, X, Y and i can make it work with the alphabet but when i wanna jump some letters i get into troubles. Can someone help me with that?

plot(x=0,y=0, type="n", ylim=c(0,250), xlim=c(0,8), bty="n", main = "Line 20 male 3 sec rep 2",
xlab = "Concentration", ylab = "MM above buttom", xaxt="n")
## added xaxt="n"
## and the axis code below
axis(1,at=0:7,labels=c("0","A","B","C","D","E","X","Y"))
fc <- levels(dat20m2$Conc)
for(i in 1:length(fc)){
tmp <- dat20m2[dat20m2$Conc==fc[i],]
points(y=tmp$t30.sum,x=rep(i,length(tmp$t30)))
points(y=mean(tmp$t30.sum),x=i,col="red")
}
abline(lm(t30.sum~as.integer(Conc),data=dat20m2), lty=2)
I got the answer from here and I've amended your code by adding the two parts described by the double pound signs. Hope this helps.

Related

Density curve on histogram is flat

I am trying to plot a curve that follows the trend of the histogram of my data, I have looked around and have tried out other peoples code but I still get a flat line. Here is my code
hist(Ferr,xlab = "Ferritin Plasma Concentration", ylab = "Frequency", main = "Histogram of Ferritin
Plasma Concentration", xlim = c(0,250), ylim = c(0,50), cex.axis=0.8, cex.lab=0.8,cex.main = 1)
curve(dnorm(x, mean = mean(Ferr), sd = sd(Ferr)), col="blue", add=TRUE)
lines(density(Ferr), col="red")
If anyone can help me to see where I have gone wrong, that would be great thank you.
Unlike an histogram, the integral of a density function over the whole space is equal to 1 :
sum(density(x)*dx) = 1
To scale the density function to the histogram, you can multiply it by the maximum value of the histogram bins and divide it by the distance between points.
Let's take mtcars$mpg as example:
Ferr <- mtcars$mpg
d <- density(Ferr)
dx <- diff(d$x)[1]
sum(d$y)*dx
[1] 1.000851
h <- hist(Ferr)
lines(x=d$x,y=max(h$counts)*d$y/dx)
You need to set freq = FALSE (and remove the constraints on ylimand xlim and change "Frequency" to "Density"):
hist(Ferr,
freq= FALSE,
xlab = "Ferritin Plasma Concentration", ylab = "Density",
main = "Histogram of Ferritin Plasma Concentration",
cex.axis=0.8, cex.lab=0.8,cex.main = 1)
curve(dnorm(x, mean = mean(Ferr), sd = sd(Ferr)), col="blue", add=TRUE)
lines(density(Ferr), col="red")
Toy data:
Ferr <- rnorm(1000)

format of the x-axis from 1 e+06 to 1 Mio

i want to change the format of the x-axis of a plot in R from 1 e+06 to 1 Mio.
What is the right comand to do that?
Thank you for your help.
Jonas
Assuming you work with a base R scatter plot and further assuming you mean what you say, namely that you want labels like "1 Mio" (instead of the number 1000000) on the x-axis, this could work for you:
DATA:
x <- seq(1000000, 10000000, 1000000)
y <- 1:10
SOLUTION:
plot(x,y, xaxt = "n")
axis(1, at = x, labels = paste(1:10, "Mio", sep = " "), las = 2)
This solution avoids printing the default x-axis labels by setting xaxt = "n"and by defining a customized x-axis including the desired axis labels

Barplot: Greek letters on y axis in R

This is a follow-up question to my other question on barplots:
Tornado plot in R
I realized the question about getting greek letters on the y-axis needed to be asked as an own question.
The question is:
I have the following barplot and need to change the y-axis to respective greek letters (and a V with a bar over).
I use the following code:
# Tornado plot
data <- matrix(c(-0.02,0.02,-0.01,0.01,-0.03,0.02,-0.01,0.04), ncol = 4)
rownames(data) <- c('+10%','-10%')
colnames(data) <- c('V_bar', 'alpha', 'rho','xi')
x <- seq(-0.04,0.04, length=10)
barplot(data[1,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n', ylab='',
beside=T, col=c('springgreen'))
barplot(data[2,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n',
yaxt='n', #To prevent double printing of y-labels.
beside=T, col=c('indianred2'), add = TRUE)
axis(1, at=pretty(x), lab=paste0(pretty(x) * 100," %"), las=TRUE)
To get the greek letters I have tried the following:
barplot(data[2,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n',
yaxt= c(expression(bar(V)), expression(alpha),expression(rho), expression(xi)),
beside=T, col=c('indianred2'), add = TRUE))
and
axis(2, at=c(1:4), lab = expression(xi ~ rho ~ alpha ~ bar(V)), las=T)
or
axis(2, at=pretty(x), lab = paste0(expression(xi ~ rho ~ alpha ~ bar(V)), las=T))
But no success. Anyone now the trick?
Note. I have seen this question: Adding greek character to axis title
But it focuses on the labels, not the y-axis "values".
Also, I have tried something like Putting greek letters in column names
with no success.
There's no need to call axis for labeling of the bars if you provide the argument names.arg to barplot:
barplot(data[1,], horiz = T, las=1, xlim = c(-0.04,0.04), xaxt='n', ylab='',
beside=T, col=c('springgreen'),
names.arg=c(expression(xi),expression(rho), expression(alpha), expression(bar(V))))
You just need to pass lab in axis as a vector of expressions.
axis(2, at=c(1:4), lab = c(expression(xi),
expression(rho), expression(alpha), expression(bar(V))), las=T)
Then you can play with the settings of the axis as needed.

Manipulating x axis labelling for a feather plot

I have some wind speed and direction data over a course of some time and I need to plot it into a feather plot.
After surfing the web for some time, I find a function someone wrote to plot the feather plot that works for me (Thank you if you are reading this!!). My problem now is that I don't know how to manipulate the labelling of the x-axis.
After the plotting, the figure looks like this:
Now the x-axis doesn't look too bad here, but imagine I have 200 data points (and thus ticks) instead of 10, and the axis ticks can get a bit confusing. So I was hoping someone can help me manipulate the x-axis, specifically messing with the ticks.
The code to plot the figure is:
stg <- scan(what="", sep="\n")
9/20/15_12:00 2.597058824 157.9411765
9/21/15_0:00 2.177192982 185.1754386
9/21/15_12:00 2.577391304 189.2173913
9/22/15_0:00 1.984955752 237.4336283
9/22/15_12:00 3.993859649 252.6315789
9/23/15_0:00 1.613392857 175.5357143
9/23/15_12:00 3.849166667 216.8333333
9/24/15_0:00 2.138135593 117.0338983
9/24/15_12:00 3.32605042 216.302521
9/25/15_0:00 1.490178571 239.8214286
df <- read.table(textConnection(stg), sep="")
colnames(df) <- c("Time", "wsp", "wdir")
df$PTime <- as.POSIXct(df$Time, format="%m/%d/%y_%H:%M")
feather.plot2 <- function (r, theta, xpos, yref = 0, use.arrows = TRUE, col.refline = "lightgray",
fp.type = "s", main = "", xlab = "", ylab = "", xlabels = NULL,
...)
{
if (missing(xpos))
xpos <- 1:length(theta)
if (fp.type == "m")
theta <- 5 * pi/2 - theta
x <- r * cos(theta)
y <- r * sin(theta)
xmult <- diff(range(xpos))/(diff(range(y)) * 2)
x <- x * xmult
xlim <- range(c(xpos, x + xpos))
ylim <- range(c(y, yref))
oldpin <- par("pin")
xdiff <- xlim[2] - xlim[1]
ydiff <- ylim[2] - ylim[1]
plot(0, xlim = xlim, ylim = ylim, type = "n", main = main,
xlab = xlab, ylab = ylab, axes = TRUE, xaxt = "n")
box()
if (is.null(xlabels))
axis(1)
else axis(1, at = xpos, labels = xlabels)
abline(h = yref, col = col.refline)
if (use.arrows)
arrows(xpos, yref, xpos + x, y, length = 0.1, ...)
else segments(xpos, yref, xpos + x, y, ...)
par(pin = oldpin)
}
feather.plot2(df$wsp, df$wdir, fp.type="m", xlabels=df$PTime)
And what I want is something like having big ticks for 12:00, and smaller ticks for 0:00, like in this figure:
Although I don't know why the label for this figure comes out as "Sun - Thu" instead of dates...
The code for this figure is:
daterange=c(min(df$PTime), max(df$PTime))
plot(x=df$PTime, y=df$wsp, xaxt="n", type="l")
axis.POSIXct(1, at=seq(daterange[1], daterange[2], by="day"))
axis.POSIXct(1, at=seq(daterange[1], daterange[2], by="12 hours"), tcl = -0.3, labels=FALSE )
I've tried using using these axis commands on the feather plot, but it did not work. So I'd appreciate any help/advice. Thank you so much!!
I'm seeing two requests: Major and minor ticks; and More compact axis annotation of date-times. Step 1: Suppress the default axis creation. Step 2: The usual manor is to label the major ticks, so we would determine the proper location of those ticks and give a format specification to the labels. Step 3: place the minor tick marks. Most of this you've already figured out, and I would have thought the format problem was the easiest one to solve, so let's see:
plot(x=df$PTime, y=df$wsp, xaxt="n", type="l")
axis.POSIXct(1, at=seq(daterange[1], daterange[2], by="day"), format="%m-%d %H%P",
lwd.ticks=2)
axis.POSIXct(1, at=seq(daterange[1], daterange[2], by="12 hours"),
tcl = -0.3, labels=FALSE )
Seems to succeed at what I think are your goals. The use of by = "day" may be what leads the interpreter to choose the three letter abbrev of day names. (I don't really know.)

Plot area too short for Y axis (base graphics)

I have created this data frame:
seq(1,70)
Group <-paste("a", 1:70, sep="")
Counts <- c(1:18, 5:14, 1:20, 5:20, 10:15)
When plotted, it returns a large plot where "Group" does not fit:
barplot(Counts, names.arg=Group,
horiz=TRUE, las=1, cex.names=0.6, border=NA,
ylim=c(0,30), xlim=c(0,20), width = 1.5)
EDIT: this is the plot after deleting ylim=c(0,30)
I would like to be able to see each term on the "Group" axis.
The ylim argument is constraining how many groups are shown. Remove it and you can see all of them.
I tried:
barplot(stage1_90$Percentage.stage1,
names.arg=stage1_90$miR.stage1,
horiz=TRUE, las=1, cex.names=0.5, border=NA,
ylim=c(0,125), xlim=c(0,14), main = "Stage 1. Top 90%",
xlab = "% total expression", space = 1, yaxs = "i")
And then I printed out in A4 format

Resources