R:如何在图中设置以弧度为单位的 y 轴?
R: How can I set the y axis in radians in a plot?
我想绘制两个向量(x 和 y)的分数的反正切值:
atan.add <- atan(x/y)
plot(atan.add)
# I get this:
但是,在 R 中有没有办法将 y 轴数字作为 pi 的分数,即 pi/2、pi/4 等?
您可以在 plot()
函数中设置 axes=FALSE
,然后使用 axis()
定义您自己的坐标轴。类似于:
x <- 1:100
set.seed(121)
y <- rnorm(5) #random data
atan.add <- atan(x/y)
plot(atan.add,axes=FALSE) #note the "axes=FALSE"
axis(side=1) #plot x axis (side=1)
axis(side=2, at=c(-pi, -pi/2, -pi/4, 0 ,pi/4, pi/2, pi), labels=expression(-pi, -pi/2, -pi/4, 0, pi/4, pi/2, pi)) #"side=2" specifies "y" axis
有关详细信息,请查看 Plot Annotation in R or Axes and Text in R Plot。
x <- 1:20
set.seed(42)
y <- rnorm(20)
atan.add <- atan(x/y)
breaks_pi <- pretty(range(atan.add/pi))
plot(atan.add, yaxt="none")
axis(2, at = breaks_pi * pi, labels = paste(breaks_pi, "\u03c0"))
我想绘制两个向量(x 和 y)的分数的反正切值:
atan.add <- atan(x/y)
plot(atan.add)
# I get this:
但是,在 R 中有没有办法将 y 轴数字作为 pi 的分数,即 pi/2、pi/4 等?
您可以在 plot()
函数中设置 axes=FALSE
,然后使用 axis()
定义您自己的坐标轴。类似于:
x <- 1:100
set.seed(121)
y <- rnorm(5) #random data
atan.add <- atan(x/y)
plot(atan.add,axes=FALSE) #note the "axes=FALSE"
axis(side=1) #plot x axis (side=1)
axis(side=2, at=c(-pi, -pi/2, -pi/4, 0 ,pi/4, pi/2, pi), labels=expression(-pi, -pi/2, -pi/4, 0, pi/4, pi/2, pi)) #"side=2" specifies "y" axis
有关详细信息,请查看 Plot Annotation in R or Axes and Text in R Plot。
x <- 1:20
set.seed(42)
y <- rnorm(20)
atan.add <- atan(x/y)
breaks_pi <- pretty(range(atan.add/pi))
plot(atan.add, yaxt="none")
axis(2, at = breaks_pi * pi, labels = paste(breaks_pi, "\u03c0"))