如何反转绘图上的 y 轴 [英] How to invert the y-axis on a plot
问题描述
我想知道如何在 R 中绘制 y 轴反转的图,以便绘制的数据出现在第四象限(中的小日志刻度函数:
minor.ticks.axis <- function(ax,n,t.ratio=0.5,mn,mx,...){lims <- par("usr")if(ax %in%c(1,3)) lims <- lims[1:2] else lims[3:4]Major.ticks <- 漂亮(lims,n=5)if(missing(mn)) mn <- min(major.ticks)if(missing(mx)) mx <- max(major.ticks)major.ticks <- major.ticks[major.ticks >= mn &Major.ticks <= mx]标签 <- sapply(major.ticks,function(i)as.expression(bquote(10^ .(i))))轴(ax,at=major.ticks,labels=labels,...)n <- n+2未成年人 <- log10(pretty(10^major.ticks[1:2],n))-major.ticks[1]未成年人 <- 未成年人[-c(1,n)]minor.ticks = c(outer(minors,major.ticks,`+`))minor.ticks <- minor.ticks[minor.ticks >和小滴答声
制作一些可重现的示例数据:
x <- 1:8y <- 10^(sort(runif(8, 1, 10), 递减 = TRUE))
没有轴的绘图:
plot(x, log10(y), # 绘图函数xlab="", # 抑制 x 个标签type = 'l', # 指定折线图xlim = c(min(x), (max(x)*1.3)), # 扩展轴限制为文本注释留出空间ylim = c(0, max(log10(y))), # 同上轴 = FALSE) # 抑制两个轴
添加花哨的对数轴并将刻度标签向右旋转(感谢@joran!):
minor.ticks.axis(2, 9, mn=0, mx=10, las=1)
在顶部添加 x 轴:
轴(3)
添加 x 轴标签(感谢提示,@WojciechSobala)
mtext("x", side = 3, line = 2)
并在行尾添加注释
text(max(x), min(log10(y)), "示例", pos = 1)
结果如下:
I would like to know how to make a plot in R where the y-axis is inverted such that the plotted data appears in what would be the fourth quadrant (IV) of a cartesian plane, as opposed to the first (I) quadrant.
For reference, the plot I am trying to make looks very similar to the following (source):
I have found a number of questions online pertaining to reversing the numbering on the y-axis, but these all still plot the data in the first quadrant. Can anyone suggest how I might produce a plot similar to that shown above?
Just to provide a worked out answer, following the comments of @timriffe and @joran...
Use the function for minor log ticks from this answer:
minor.ticks.axis <- function(ax,n,t.ratio=0.5,mn,mx,...){
lims <- par("usr")
if(ax %in%c(1,3)) lims <- lims[1:2] else lims[3:4]
major.ticks <- pretty(lims,n=5)
if(missing(mn)) mn <- min(major.ticks)
if(missing(mx)) mx <- max(major.ticks)
major.ticks <- major.ticks[major.ticks >= mn & major.ticks <= mx]
labels <- sapply(major.ticks,function(i)
as.expression(bquote(10^ .(i)))
)
axis(ax,at=major.ticks,labels=labels,...)
n <- n+2
minors <- log10(pretty(10^major.ticks[1:2],n))-major.ticks[1]
minors <- minors[-c(1,n)]
minor.ticks = c(outer(minors,major.ticks,`+`))
minor.ticks <- minor.ticks[minor.ticks > mn & minor.ticks < mx]
axis(ax,at=minor.ticks,tcl=par("tcl")*t.ratio,labels=FALSE)
}
Make some reproducible example data:
x <- 1:8
y <- 10^(sort(runif(8, 1, 10), decreasing = TRUE))
Plot without axes:
plot(x, log10(y), # function to plot
xlab="", # suppress x labels
type = 'l', # specify line graph
xlim = c(min(x), (max(x)*1.3)), # extend axis limits to give space for text annotation
ylim = c(0, max(log10(y))), # ditto
axes = FALSE) # suppress both axes
Add fancy log axis and turn tick labels right way up (thanks @joran!):
minor.ticks.axis(2, 9, mn=0, mx=10, las=1)
Add x-axis up the top:
axis(3)
Add x-axis label (thanks for the tip, @WojciechSobala)
mtext("x", side = 3, line = 2)
And add an annotation to the end of the line
text(max(x), min(log10(y)), "Example", pos = 1)
Here's the result:
这篇关于如何反转绘图上的 y 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!