r - 如何更改 ggplot2 中 x 和 y 轴的位置

标签 r ggplot2

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

关注 Kohske Post Here ,使用的命令是:

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

我在开发模式下尝试了上述命令:
install_packages("devtools")
library(devtools)
dev_mode()
install_github("ggplot2", "kohske", "feature/pguide")
library(ggplot2) 

不幸的是,它不适用于最新的 plyr包裹。留言:
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)

然后我尝试了codes from github直接发送的消息是:
Error in continuous_scale(c("x", "xmin", "xmax", "xend", "xintercept"),  : 
  formal argument "guide" matched by multiple actual arguments

我注意到 Hadley 说这个功能在他的待办事项 list 上。但是,此时我找不到解决方案。有人可以帮忙吗?

最佳答案

来自 ggplot 2.2.0 您可以使用 position 设置轴的位置参数在 scale_ :

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

enter image description here

关于r - 如何更改 ggplot2 中 x 和 y 轴的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15334494/

相关文章:

r - 在 Ubuntu 上找不到 RODBC 包中的 odbcConnectExcel 函数

r - 如何在 R 中为条件逻辑回归匹配患者数据?

r - 在 dplyr 中使用重命名并且列不存在时避免错误

r - 如何避免由于舍入错误导致的 NaN

r - ggplot2 关于我无法使用 `na.rm=T` 禁用的缺失的警告

r - geom_label 不会相应地缩放以在 Shiny 中绘制

r - 将 ggplot 保存为 EPS 格式以便在 Adob​​e Illustrator 中进行编辑——文本问题

r - dplyr 不按日期对数据进行分组

r - 使用命令行时无法通过 openxlsx 包将绘图插入 XLSX

R 图形 : output to several file formats