如何更改 ggplot2 中 x 和 y 轴的位置 [英] How to change positions of x and y axis in ggplot2

查看:193
本文介绍了如何更改 ggplot2 中 x 和 y 轴的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的真实研究世界中,在顶部(或顶部和底部)显示 x 轴而在右侧显示 y 轴是很常见的.但是,ggplot2 中的默认位置是底部的 x 和左侧的 y.

按照

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?

解决方案

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

这篇关于如何更改 ggplot2 中 x 和 y 轴的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆