Benoit B.
Benoit B.

Reputation: 12044

R flip XY axis on a plot

This seems like a trivial R question, but I didn't find any convincing solution. I would like to flip my plot where the X axis become Y, and vice-versa. In boxplot there is an horiz="T" option, but not in plot().

This is what I plot :

plot(rm, type="l", main="CpG - running window 100")

> str(rm)
 num [1:43631] 0.667 0.673 0.679 0.685 0.691 ...

This is what I have

And I would like to obtain this :

enter image description here

Thanks for the feedback.

Upvotes: 5

Views: 15662

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193507

I think the problem is because the plot doesn't explicitly have an index. Try the following:

set.seed(1)
a = rnorm(200) # like your `rm` -- bad name for an object, by the way
plot(a, type="l", main="rnorm(200)") # index automatically added

This is similar what you have. It is also equivalent to plot(1:length(a), a, ...) where 1:length(a) is your x and a is your y.

enter image description here

Keeping the above in mind, we can flip your chart like this:

# index specified, and X-Y swapped
plot(a, 1:length(a), type="l", main="rnorm(200)") 

enter image description here

Upvotes: 3

Related Questions