Change baseline y to 1 in barplot (instead of zero) - r

I want to present a barplot with baseline y=1. I want to present fold change, therefore starting with 1. How do I change y starting value with the function barplot? Thanks!
a <- c(0.5,1.5)
barplot(a)

Simulate a new y axis baseline by subtracting 1 and then compensating in the axis labels.
a <- c(0.5,1.5)
at <- c(-0.5, 0, 0.5, 1)
barplot(a - 1, yaxt = "n")
axis(2, at = at, labels = at + 1)
abline(h = 0)
Created on 2022-10-17 with reprex v2.0.2

You could subset on values greater than or equal to 1, use ylim together with xpd. This 1. does not show FC < 1 and the plot has baseline at 1.
barplot(a[a >= 1], ylim=c(1, max(a)*1.1), xpd=FALSE)
box()
Data:
set.seed(334322)
a <- runif(10, 0, 6)

The direct solution is
barplot(a - 1, offset = 1)
Although, being fold changes, consider whether it may be better to use log2 scales or transformations.

Related

ggplot - altering the height of each overlapping variable on a density plot

I'm quite new to R and ggplot2 so apologies if this is an obvious question, but I've searched around and can't find anything about this exact issue
I have a ggplot density plot for 6 variables on the same plot, overlapping. What I am trying to do is to change the maximum height of each variable to be a certain value without changing the distribution. e.g. :
variable_1 - 1, //on Y axis
variable_2 - 0.5 etc.
This way I can get an idea of the distribution (across the x axis) whilst also showing a second independent parameter through the y axis
Is this possible at all?
Yes this is possible although I wouldn't recommend it. What you can do is just divide the distribution by it's maximum and then multiply with the target height.
# some example data:
x = seq(-5, 5, .1)
y1 = dnorm(x)
y2 = dnorm(x, .5, .2)
Y = cbind(y1, y2)
matplot(x, Y, type = 'l', bty = 'n', lty = 1, las = 1)
# now I want the red line to be max 1
# and the black line to be mack .5
y1 = .5*y1 / max(y1)
y2 = 1*y2 / max(y2)
Y = cbind(y1, y2)
matplot(x, Y, type = 'l', bty = 'n', lty = 1, las = 1)
The important part here is that I used two different transformations for y1 and y2. The consequence is that in the second figure the distributions cannot be compared anymore. You can avoid this by only applying the same transformation to all distributions.

Plot a piecewise regression in two steps, in the same plot

I'm trying to plot two curves in the same graph, but it doesn't work. I want to plot the function f(x) = 3x + 2 if x<=3 and f(x) = 2x-0.5x^2 if x>3 on the interval [0,6]. I thought I had to do
curve(3*x+2, 0,3)
and
curve(2*x-0.5*x^2,3,6, add = TRUE)
What could I do to plot such function?
Use xlim and ylim in the first curve to set the limits of the plot.
curve(3*x+2, 0,3, xlim = c(0, 6), ylim = c(-5, 12))
curve(2*x-0.5*x^2,3,6, add = TRUE)
As the second curve still gets cut off a little bit, you might want to use c(-7 12) for the y limits.
Another option, if you want the lines connected and which removes the need to set manual limits is to encode both functions in one with ifelse:
curve(ifelse(x <= 3, 3 * x + 2, 2 * x - 0.5 * x^2), 0, 6, ylab = "f(x)")
You can do this for instance, you'll get 2 curves together.
c1 <- curve(2*x-0.6*x^2,3,6)
c2 <- curve(2*x-0.5*x^2,3,6)
plot(c1)
lines(c2, col="red")

How fit a point on a ecdf curve

I have a random serie of values, a <- runif(1000, 0, 10), and a single value: b <- 1.5 . I want to plot the ecdf, and put on it the value to see the probability to have it.
But when i do that:
plot(ecdf(a))
points(b, col = 'red', lwd = 2)
The points doesn't appear. I think i have to fit the points with the curve to find the y axis value which correspond to it, but i don't really know how to do it. If someoane can help me. Thanks!
ecdf returns a function. You can do this:
set.seed(42)
a <- runif(1000, 0, 10)
b <- 1.5
e <- ecdf(a)
plot(e)
points(b, e(b), col = 'red', cex= 2)

R - adjust the y-value of the x axis

I have a some data that I want to display graphically. Here's what it looks like:
data<- c(0.119197746, 0.054207788, 0.895580411, 0.64861727, 0.143249592,
0.284314897, 0.070027632, 0.297172433, 0.183569184, 0.713896071,
1.942425326, 1)
Using this command:
barplot(data, main="Ratio of Lipidated and Unlipidated LC3 I & II forms\nNormalized
to GAPDH", names.arg = c("PT250", "PT219", "PT165", "PT218", "PT244", "PT253", "PT279", "PT281",
"PT240", "PT262", "PT264", "CCD"), ylab = "Fold LC3 II/LC3I/GAPDH")
I produced this graph:
I would like to position the X-axis at 1 so that all values less-than-one will appear as down bars. I could achieve the desired affect by simply subtracting 1 from all of the values and plotting again but this would cause the numbers on the y-axis to be inaccurate. Is there some way to get R to plot values less than 1 as down bars?
Solution with custom axis.
barplot(data - 1, main="Ratio of Lipidated and Unlipidated LC3 I & II forms\nNormalized
to GAPDH", names.arg = c("PT250", "PT219", "PT165", "PT218", "PT244", "PT253", "PT279", "PT281",
"PT240", "PT262", "PT264", "CCD"), ylab = "Fold LC3 II/LC3I/GAPDH",
axes = F, ylim = c(-1, 1)
my_labs <- seq(-1, 1, by = 0.5)
axis(side = 2, at = my_labs, labels = my_labs + 1)

Combining 2 datasets in a single plot in R

I have two columns of data, f.delta and g.delta that I would like to produce a scatter plot of in R.
Here is how I am doing it.
plot(f.delta~x, pch=20, col="blue")
points(g.delta~x, pch=20, col="red")
The problem is this: the values of f.delta vary from 0 to -7; the values of g.delta vary from 0 to 10.
When the plot is drawn, the y axis extends from 1 to -7. So while all the f.delta points are visible, any g.delta point that has y>1 is cut-off from view.
How do I stop R from automatically setting the ylims from the data values. Have tried, unsuccessfully, various combinations of yaxt, yaxp, ylims.
Any suggestion will be greatly appreciated.
Thanks,
Anjan
In addition to Gavin's excellent answer, I also thought I'd mention that another common idiom in these cases is to create an empty plot with the correct limits and then to fill it in using points, lines, etc.
Using Gavin's example data:
with(df,plot(range(x),range(f.delta,g.delta),type = "n"))
points(f.delta~x, data = df, pch=20, col="blue")
points(g.delta~x, data = df, pch=20, col="red")
The type = "n" causes plot to create only the empty plotting window, based on the range of x and y values we've supplied. Then we use points for both columns on this existing plot.
You need to tell R what the limits of the data are and pass that as argument ylim to plot() (note the argument is ylim not ylims!). Here is an example:
set.seed(1)
df <- data.frame(f.delta = runif(10, min = -7, max = 0),
g.delta = runif(10, min = 0, max = 10),
x = rnorm(10))
ylim <- with(df, range(f.delta, g.delta)) ## compute y axis limits
plot(f.delta ~ x, data = df, pch = 20, col = "blue", ylim = ylim)
points(g.delta ~ x, data = df, pch = 20, col = "red")
Which produces

Resources