Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

r - How to change positions of x and y axis in ggplot2

In my real research world, it is very common to show x-axis on the top (or both top and bottom) and y-axis on the right. However, the default positions are x on the bottom and y on the left in ggplot2.

Following Kohske Post Here, the commands used are:

x <- seq(0, 10, 0.1)
y <- sin(x * pi)
qplot(x, y, geom = "line") + 
scale_x_continuous(guide = guide_axis(position = "top")) + 
scale_y_continuous(guide = guide_axis(position = "right"))

I have tried above commands in dev-mode:

install_packages("devtools")
library(devtools)
dev_mode()
install_github("ggplot2", "kohske", "feature/pguide")
library(ggplot2) 

Unfortunately, it didn't work well with the latest plyr package. Messages:

The following 'from' values not present in 'x': col, color, pch, cex, lty, lwd, srt, adj, bg, fg, min, max... 
Error in plyr:::split_indices(seq_len(nrow(data)), scale_id, n)

Then I tried the codes from github directedly, the messages are:

Error in continuous_scale(c("x", "xmin", "xmax", "xend", "xintercept"),  : 
  formal argument "guide" matched by multiple actual arguments

I have noticed that Hadley said this functionality is on his to-do list. However, I could not find a solution at this moment. Could anyone help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From ggplot 2.2.0 you can set the position of the axes with the position argument in scale_:

ggplot(mpg, aes(displ, hwy)) + 
  geom_point() + 
  scale_x_continuous(position = "top") + 
  scale_y_continuous(position = "right")

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...