DavideChicco.it
DavideChicco.it

Reputation: 3470

R, how to move the plot axis origin in ggplot2?

I'm creating a plot with ggplot2 in R. The 0.0 origin point is in the left-lower corner as usual, but I would like to move it to the upper-left corner and reverse the order of the points.

I try to explain my intention with the following image. I want to move the 0.0 origin to the red 0.0 origin point. Of course, also the content of the y axis points must be reversed.

enter image description here To create the plot, I'm using:

P = ggplot(plot_data_frame, aes(x=Index, y=dataVector)) + geom_point() + geom_line()

How could I do this? Thanks!

Upvotes: 1

Views: 2892

Answers (2)

shrgm
shrgm

Reputation: 1344

You can use the scale_y_reverse option.

P = ggplot(plot_data_frame, aes(x=Index, y=dataVector)) + geom_point() + geom_line()

P + scale_y_reverse()

If you want specific limits, you can do so using:

P + scale_y_reverse(lim=c(10000,0))

Upvotes: 3

paljenczy
paljenczy

Reputation: 4899

Simply add

P <- P + ylim(40000, 0)

if, for example, you choose 40000 to be the upper limit. An example:

ggplot(data = mtcars, aes(x = mpg, y = gear)) + geom_point()

produces

enter image description here whereas

ggplot(data = mtcars, aes(x = mpg, y = gear)) + geom_point() + ylim(5, 3)

produces

enter image description here

Upvotes: 2

Related Questions