Mryqu's Notes


  • 首页

  • 搜索
close

在R作图系统中自定义坐标轴

时间: 2014-04-15   |   分类: DataScience     |   阅读: 238 字 ~2分钟

在R作图系统中自定义坐标轴

R作图系统中坐标中的标签默认是等间隔,下列尝试是为了将少量数据的坐标显示在坐标轴上。

加载库并定义数据

library(lattice)
library(ggplot2)
library(grid)
library(gridExtra)

x <- c(1, 2, 3, 7, 8, 9)
y <- c(1, 23, 12, 77, 14, 2)
data <- data.frame(x = x, y = y)
data
##   x  y
## 1 1  1
## 2 2 23
## 3 3 12
## 4 7 77
## 5 8 14
## 6 9  2

Base作图系统

opar <- par(mfrow = c(1, 2), mar = c(4, 6, 4, 2))
with(data, plot(x, y, type = "b", main = "Default Axis"))

par(las = 1)
with(data, plot(x, y, type = "b", main = "customised Axis", xaxt = "n", yaxt = "n"))
axis(1, data$x)
axis(2, data$y)

plot of chunk unnamed-chunk-2

par(opar)

Lattice作图系统

plot1 <- xyplot(y ~ x, data, type = "b", main = "Default Axis")
plot2 <- xyplot(y ~ x, data, type = "b", main = "customised Axis", scales = list(x = list(at = unlist(data$x)), 
    y = list(at = unlist(data$y))), las = 1)
grid.arrange(plot1, plot2, ncol = 2)

plot of chunk unnamed-chunk-3

ggplot2作图系统

vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))

g1 <- ggplot(data, aes(x, y)) + geom_point() + labs(title = "Default Axis")

g2 <- ggplot(data, aes(x, y)) + geom_point() + labs(title = "customised Axis") + 
    scale_x_continuous(breaks = x)

print(g1, vp = vplayout(1, 1))
print(g2, vp = vplayout(1, 2))

plot of chunk unnamed-chunk-4

引用

Controlling Axes of R Plots
Getting rid of axis values in R Plot
rotate ylab
Custom lattice axis scales
lattice坐标系和坐标轴
ggplot2: customised X-axis ticks
Combined plot of ggplot2
Quick-R Graph

标题:在R作图系统中自定义坐标轴
作者:mryqu
声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!

#r# #base# #lattice# #ggplot2# #axis#
[Hadoop] Failed to exec (compile-ms-winutils) on project hadoop-common
谷歌拼音输入法-笔划/组件输入
  • 文章目录
  • 站点概览

Programmer & Architect

662 日志
27 分类
1472 标签
GitHub Twitter FB Page
  • 加载库并定义数据
  • Base作图系统
  • Lattice作图系统
  • ggplot2作图系统
  • 引用
© 2009 - 2023 Mryqu's Notes
Powered by - Hugo v0.120.4
Theme by - NexT
0%