how to customize y-axis step size in R - r

I would like to plot a scatterplot with y-axis is customized to step size of 0.2, within range of 0 - 2.6, and x-axis can be auto-defined. I tried the below, but it doesnt work. May I know how should I set the param correctly?
# Read data
pt.n <- read.table("p0_n300m20r1c1_regression.txt", header=T)
# auto-scale
# plot(pt.n$maee~pt.n$idx, main="P2PSim Seq#1,300n,20%,1r,Corrective", ylab="MAEE", xlab="Seq #")
# customize
ylabel <- c(0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6)
y_range <- range(0, ylabel)
plot(pt.n$maee~pt.n$idx, main="P2PSim Seq#3,300n,20%,1r,Corrective", ylab="MAEE", xlab="Seq #", ylim=y_range, axes=FALSE, ann=FALSE)
axis(1, at=0:6, lab=c(0,50,100,150,200,250,300))
axis(2, las=1, at=0.2*0:y_range[1])
box()

If something is not working check each bit of the thing that isn't doing what you want to make sure you are supplying the correct data and haven't made a booboo. If we run the bits of your code that are associated with the axis
ylabel <- c(0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6)
y_range <- range(0, ylabel)
0.2*0:y_range[1]
You would immediately see the problem:
R> 0.2*0:y_range[1]
[1] 0
where you are basically telling R to draw a tick at 0. Even if you chose the correct element of y_range (the maximum is in the second element) you still wouldn't get the right answer:
R> 0.2*0:y_range[2]
[1] 0.0 0.2 0.4
R> 0:y_range[2]
[1] 0 1 2
and that is because of the way the : operator works. A call of x:y is essentially a call to seq(from = x, to = y, by = 1) and because 2.6+1 is greater than 2.6 (the to argument) R creates the sequence 0, 1, 2.
If you want to draw ticks and label at 0 - 2.6 incrementing by 0.2 then use:
ylabel <- seq(0, 2.6, by = 0.2)
axis(2, at = ylabel)
where ylabel now contains:
R> ylabel
[1] 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6
To illustrate:
dat <- data.frame(y = runif(20, min = 0, max = 3),
x = rnorm(20))
plot(y ~ x, data = dat, axes = FALSE)
ylabel <- seq(0, 2.6, by = 0.2)
axis(1)
axis(2, at = ylabel, las = 1)
box()
which produces

Related

Forcing sunflower plot to start at x=0

When creating a sunflower plot for logistic regression, the x-axis starts at 2. Can I change this behaviour to make the x-axis start at 0? I've already tried to manually change this by changing the x-axis, but that didn't make x=0 visible (see #c1).
The Sunflower Plot Image as it currently is, starting at x=2;
# EE contains the Likert Scale values
EE.min <- min(EE)
EE.max <- max(EE)
EE.x <- seq(EE.min, EE.max, length = 500)
New.EE <- data.frame(EE = EE.x)
# Creating prediction
EE.p <- predict(logit, New.EE, type = "response")
sunflowerplot(EE, cb, main = "Effort Expectancy",
xlab= "EE (5-point Likert-Scale)", ylab="Likelihood", yaxt="n", xaxt="n")
# c1:
axis(1, at = seq(0,5,0.5), labels = c(0, 0.5, 1, 1.5, 2, 2.5 , 3, 3.5, 4, 4.5, 5), las=1)
axis(2, at = seq(0,1,0.2), labels = c("No = 0", 0.2, 0.4, 0.6, 0.8, "Yes = 1"), las = 2)
abline(h = seq(0,1,0.2), lty = 2)
lines(EE.x, EE.p)
sunflowerplot has an argument xlim for the limits of the x axis.
Compare
sunflowerplot(iris$Sepal.Length, iris$Sepal.Width)
to
sunflowerplot(iris$Sepal.Length, iris$Sepal.Width, xlim = c(0, 20))

Scale in plot graph from RStudio only shows labels inbetween on y axis and leaves out the ends 5, and -25

y = c(2.9, 3.1, −1.2, −1.1, −3.3 ,3.7 ,1.9 ,−0.3, −5.9, −7.9,
−5.5, −7.2, −4.1 ,−8.6, −5.5, −0.7, −5.1, −7.1, −4.2,
0.9, −6.1, −4.1, −4.8, −11.3 −9.3, −10.7, −1.8, −7.4, −22.9)
x = c(1971:1999)
plot(x, y)
I'm new to R and can't seem to figure out how to make the labels inclusive for the whole y range. The first and last numbers appear to be excluded by default?
You can simply add ylim = c(-25, 5) to the plot call:
y = c(2.9, 3.1, −1.2, −1.1, −3.3 ,3.7 ,1.9 ,−0.3, −5.9, −7.9,
−5.5, −7.2, −4.1 ,−8.6, −5.5, −0.7, −5.1, −7.1, −4.2,
0.9, −6.1, −4.1, −4.8, −11.3 −9.3, −10.7, −1.8, −7.4, −22.9)
x = c(1972:1999)
plot(x, y, ylim = c(-25, 5))

Draw frequency density histogram in R

Using R, can anyone show me how to draw a simple histogram with no gaps between the bins of the following data :-
Class Width Freq. Dist
0 <= x < 5 0.2
5 <= x < 15 0.1
15 <= x < 20 1.2
20 <= x < 30 0.4
30 <= x < 40 0.4
So I want the X axis to go from 0-5,5-15,15-20,20-30 and 30-40 with the bars drawn appropriately.
Thanks in advance !
How about this one?
breaks <- c(0,5,15,20,30,40)
counts <- c(0.2, 0.1, 1.2, 0.4, 0.4)
barplot(counts,
names=sprintf("[%g,%g)",
breaks[-length(breaks)], breaks[-1]
),
space=0
)
This will give you bars of equal widths. On the other hand, If you'd like to obtain bars of various widths, type:
barplot(counts, diff(breaks),
names=sprintf("[%g,%g)", breaks[-length(breaks)], breaks[-1]),
space=0
)
Moreover, this will give you an "ordinary" X axis:
barplot(counts, diff(breaks), space=0)
axis(1)
And if you'd like to get axis breaks exactly at points in breaks, type:
axis(1, at=breaks)
I would look into the "HistogramTools" package for R.
breaks <- c(0, 5, 15, 20, 30, 40)
counts <- c(0.2, 0.1, 1.2, 0.4, 0.4)
library(HistogramTools)
plot(PreBinnedHistogram(breaks, counts), main = "")

How to change xmax and xmin in histogram part of heatmap.2 plot from the R package gplots?

I have multiple heat maps similar to the following:
X <- matrix(nrow=3, ncol=3)
X[1,] <- c(0.3, 0.4, 0.45)
X[2,] <- c(0.3, 0.7, 0.65)
X[3,] <- c(0.3, 0.4, 0.45)
colnames(X)<-c(1.5, 3, 4)
rownames(X)<-c(1.5, 3, 4)
library(gplots)
heatmap.2( X, Rowv=NA, Colv=NA, col=rev(heat.colors(256)),
sepcolor="black", trace="none",dendrogram="none" )
Now, in order to make multiple plots of this kind look more similar, how can I get the upper left histogram to always go between 0 and 1?
Based on yuk's answer I made this version:
X <- matrix(nrow=3, ncol=3)
X[1,] <- c(0.3, 0.4, 0.45)
X[2,] <- c(0.3, 0.7, 0.65)
X[3,] <- c(0.3, 0.4, 0.45)
colnames(X)<-c(1.5, 3, 4)
rownames(X)<-c(1.5, 3, 4)
library(gplots)
colors <- rev(heat.colors(256))
colbr <- c(seq(0, 1, len=length(colors)+1))
heatmap.2(X, scale="none", col=colors, breaks=colbr,
key=T, symkey=F, density.info="histogram", trace="none", Rowv=NA, Colv=NA,
sepcolor="black", dendrogram="none" )
Now the color scale goes between 0 and 1 bit the histogram does still not.
I think the best way is to set color breaks.
Here is what i usually do (x is the matrix for heatmap):
n.col=16 # number of colors
cm = redblue(n.col) # red-white-blue colormap
mmx = min(abs(min(x)),abs(max(x))) # find min value, or you can set to a number
colbr <- c(seq(-mmx/2,mmx/2, len=length(cm)+1)) # vector of symmetric breaks
heatmap.2(x, scale="none", col=cm, breaks=colbr,
key=T, symkey=F, density.info="histogram", trace="none")

Add error bars to show standard deviation on a plot in R

For each X-value I calculated the average Y-value and the standard deviation (sd) of each Y-value
x = 1:5
y = c(1.1, 1.5, 2.9, 3.8, 5.2)
sd = c(0.1, 0.3, 0.2, 0.2, 0.4)
plot (x, y)
How can I use the standard deviation to add error bars to each datapoint of my plot?
A solution with ggplot2 :
qplot(x,y)+geom_errorbar(aes(x=x, ymin=y-sd, ymax=y+sd), width=0.25)
In addition to #csgillespie's answer, segments is also vectorised to help with this sort of thing:
plot (x, y, ylim=c(0,6))
segments(x,y-sd,x,y+sd)
epsilon <- 0.02
segments(x-epsilon,y-sd,x+epsilon,y-sd)
segments(x-epsilon,y+sd,x+epsilon,y+sd)
You can use arrows:
arrows(x,y-sd,x,y+sd, code=3, length=0.02, angle = 90)
You can use segments to add the bars in base graphics. Here epsilon controls the line across the top and bottom of the line.
plot (x, y, ylim=c(0, 6))
epsilon = 0.02
for(i in 1:5) {
up = y[i] + sd[i]
low = y[i] - sd[i]
segments(x[i],low , x[i], up)
segments(x[i]-epsilon, up , x[i]+epsilon, up)
segments(x[i]-epsilon, low , x[i]+epsilon, low)
}
As #thelatemail points out, I should really have used vectorised function calls:
segments(x, y-sd,x, y+sd)
epsilon = 0.02
segments(x-epsilon,y-sd,x+epsilon,y-sd)
segments(x-epsilon,y+sd,x+epsilon,y+sd)
A Problem with csgillespie solution appears, when You have an logarithmic X axis. The you will have a different length of the small bars on the right an the left side (the epsilon follows the x-values).
You should better use the errbar function from the Hmisc package:
d = data.frame(
x = c(1:5)
, y = c(1.1, 1.5, 2.9, 3.8, 5.2)
, sd = c(0.2, 0.3, 0.2, 0.0, 0.4)
)
##install.packages("Hmisc", dependencies=T)
library("Hmisc")
# add error bars (without adjusting yrange)
plot(d$x, d$y, type="n")
with (
data = d
, expr = errbar(x, y, y+sd, y-sd, add=T, pch=1, cap=.1)
)
# new plot (adjusts Yrange automatically)
with (
data = d
, expr = errbar(x, y, y+sd, y-sd, add=F, pch=1, cap=.015, log="x")
)

Resources