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.
我想知道如何在R中绘制一个图,其中y轴被反转,使得绘制的数据出现在笛卡尔平面的第四象限(IV)中,而不是第一个(I)象限。
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?
我在网上发现了许多关于反转y轴编号的问题,但这些问题仍然在第一象限中绘制数据。任何人都可以建议我如何制作类似于上面所示的情节?
3 个解决方案
#1
10
Just to provide a worked out answer, following the comments of @timriffe and @joran...
根据@timriffe和@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!):
添加花式日志轴并向上翻转刻度标签(感谢@joran!):
minor.ticks.axis(2, 9, mn=0, mx=10, las=1)
Add x-axis up the top:
在顶部添加x轴:
axis(3)
Add x-axis label (thanks for the tip, @WojciechSobala)
添加x轴标签(感谢提示,@ 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:
这是结果:
#2
2
Answering the question in the title, the best/easiest way to invert the axis is to flip the limit
variables around:
回答标题中的问题,反转轴的最佳/最简单方法是翻转限制变量:
> plot(1:10, xlim=c(1,10));
> plot(1:10, xlim=c(10,1));
> plot(1:10, ylim=c(10,1));
Doing it this way means that you don't need to mess around with axes that are different from the image coordinates.
这样做意味着您不需要使用与图像坐标不同的轴。
This can be combined with the 'xaxt="n"' parameter and an additional axis
command to place an axis on another side:
这可以与'xaxt =“n”'参数和另一个轴命令结合使用,将轴放在另一侧:
> plot(1:10, ylim=c(10,1), xaxt="n"); axis(3);
#3
0
It's now quite easy to reverse the y-axis using scale_y_reverse
and specify position = "top"
for the x-axis in ggplot2
现在很容易使用scale_y_reverse来反转y轴,并在ggplot2中为x轴指定position =“top”
Example
例
library(ggplot2)
library(scales)
set.seed(99)
Date <- seq(from = as.Date("2017-12-01"), to = as.Date("2017-12-15"),
by = "days")
Flux <- runif(length(Date), 1, 10000)
Flux_df <- data.frame(Date, Flux)
p1 <- ggplot(Flux_df, aes(Date, Flux)) +
geom_col() +
xlab("") +
scale_x_date(position = "top", breaks = pretty_breaks(), expand = c(0, 0)) +
scale_y_reverse(expand = expand_scale(mult = c(0.2, 0))) +
theme_bw(base_size = 16) +
theme(panel.border = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line()) +
theme(legend.position = "none")
p1
If we want both logarithmic and reverse axis, we need a workaround suggested here as ggplot2
does not have that option atm
如果我们想要对数轴和反向轴,我们需要在此处建议的解决方法,因为ggplot2没有该选项atm
reverselog_trans <- function(base = exp(1)) {
trans <- function(x) -log(x, base)
inv <- function(x) base^(-x)
scales::trans_new(paste0("reverselog-", format(base)), trans, inv,
scales::log_breaks(base = base), domain = c(1e-100, Inf))
}
p1 + scale_y_continuous(trans = reverselog_trans(10),
breaks = scales::trans_breaks("log10", function(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x)),
expand = expand_scale(mult = c(0.2, 0))) +
annotation_logticks()
#1
10
Just to provide a worked out answer, following the comments of @timriffe and @joran...
根据@timriffe和@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!):
添加花式日志轴并向上翻转刻度标签(感谢@joran!):
minor.ticks.axis(2, 9, mn=0, mx=10, las=1)
Add x-axis up the top:
在顶部添加x轴:
axis(3)
Add x-axis label (thanks for the tip, @WojciechSobala)
添加x轴标签(感谢提示,@ 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:
这是结果:
#2
2
Answering the question in the title, the best/easiest way to invert the axis is to flip the limit
variables around:
回答标题中的问题,反转轴的最佳/最简单方法是翻转限制变量:
> plot(1:10, xlim=c(1,10));
> plot(1:10, xlim=c(10,1));
> plot(1:10, ylim=c(10,1));
Doing it this way means that you don't need to mess around with axes that are different from the image coordinates.
这样做意味着您不需要使用与图像坐标不同的轴。
This can be combined with the 'xaxt="n"' parameter and an additional axis
command to place an axis on another side:
这可以与'xaxt =“n”'参数和另一个轴命令结合使用,将轴放在另一侧:
> plot(1:10, ylim=c(10,1), xaxt="n"); axis(3);
#3
0
It's now quite easy to reverse the y-axis using scale_y_reverse
and specify position = "top"
for the x-axis in ggplot2
现在很容易使用scale_y_reverse来反转y轴,并在ggplot2中为x轴指定position =“top”
Example
例
library(ggplot2)
library(scales)
set.seed(99)
Date <- seq(from = as.Date("2017-12-01"), to = as.Date("2017-12-15"),
by = "days")
Flux <- runif(length(Date), 1, 10000)
Flux_df <- data.frame(Date, Flux)
p1 <- ggplot(Flux_df, aes(Date, Flux)) +
geom_col() +
xlab("") +
scale_x_date(position = "top", breaks = pretty_breaks(), expand = c(0, 0)) +
scale_y_reverse(expand = expand_scale(mult = c(0.2, 0))) +
theme_bw(base_size = 16) +
theme(panel.border = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line()) +
theme(legend.position = "none")
p1
If we want both logarithmic and reverse axis, we need a workaround suggested here as ggplot2
does not have that option atm
如果我们想要对数轴和反向轴,我们需要在此处建议的解决方法,因为ggplot2没有该选项atm
reverselog_trans <- function(base = exp(1)) {
trans <- function(x) -log(x, base)
inv <- function(x) base^(-x)
scales::trans_new(paste0("reverselog-", format(base)), trans, inv,
scales::log_breaks(base = base), domain = c(1e-100, Inf))
}
p1 + scale_y_continuous(trans = reverselog_trans(10),
breaks = scales::trans_breaks("log10", function(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x)),
expand = expand_scale(mult = c(0.2, 0))) +
annotation_logticks()