user11916948
user11916948

Reputation: 954

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

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)

Upvotes: 3

Views: 196

Answers (3)

Ottie
Ottie

Reputation: 1030

The direct solution is

barplot(a - 1, offset = 1)

enter image description here

Although, being fold changes, consider whether it may be better to use log2 scales or transformations.

Upvotes: 0

jay.sf
jay.sf

Reputation: 72593

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()

enter image description here


Data:

set.seed(334322)
a <- runif(10, 0, 6)

Upvotes: 0

Rui Barradas
Rui Barradas

Reputation: 76402

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

Upvotes: 1

Related Questions