R plot()或ggplot2()的对数y轴刻度标记

时间:2022-10-19 09:25:47

I saw the ideal tick-mark structure for a log="y" plot in this paper, Figure 3b 3c 3d.

我在本文中看到了一个log="y"图的理想的tick-mark结构,图3b 3c 3d。

It has short, log-spaced minor tick marks without labels, plus long, log-spaced major tick marks with labels.

它有短的,对数间距的小标记,没有标签,加上长,以对数间隔的主要标记与标签。

Does anyone know how to achieve this in R?

有人知道如何在R中实现这个吗?

4 个解决方案

#1


11  

In base R just build the axes however you want. Something like this could be a start.

在基底R中,只要建立坐标轴。像这样的事情可能是一个开始。

set.seed(5)
d <- data.frame(x=1:100, y=rlnorm(100, meanlog=5, sdlog=3))
with(d, {
  plot(x, y, log="y", yaxt="n")
  y1 <- floor(log10(range(y)))
  pow <- seq(y1[1], y1[2]+1)
  ticksat <- as.vector(sapply(pow, function(p) (1:10)*10^p))
  axis(2, 10^pow)
  axis(2, ticksat, labels=NA, tcl=-0.25, lwd=0, lwd.ticks=1)
})

In lattice, the latticeExtra package has the capability:

在格网中,latticeExtra包具有以下功能:

library(lattice)
library(latticeExtra)
xyplot(y~x, data=d, scales=list(y=list(log=10)),
       yscale.components=yscale.components.log10ticks)

#2


6  

For ggplot2, it seems that the only option you have for specifying ticks is the size (i.e., width).

对于ggplot2来说,指定刻度的惟一选项是大小(也就是)。、宽度)。

# A plot of any old data
dfr <- data.frame(x = 1:100, y = rlnorm(100))
p <- ggplot(dfr, aes(x, y)) + 
  geom_point() +
  scale_y_log10(breaks = breaks, labels = breaks)


#Tick locations
get_breaks <- function(x)
{
  lo <- floor(log10(min(x, na.rm = TRUE)))
  hi <- ceiling(log10(max(x, na.rm = TRUE)))
  as.vector(10 ^ (lo:hi) %o% 1:9)
}

breaks <- get_breaks(dfr$y)
log10_breaks <- log10(breaks)

#Some bigger ticks
p + opts(axis.ticks = theme_segment(
    size = ifelse(log10_breaks == floor(log10_breaks), 2, 1)
  ))

#3


3  

This has been done in package::sfsmisc. See the example in help(axTexpr)

这是在package::sfsmisc中完成的。参见help(axTexpr)示例

#4


0  

This is implemented in version 0.6.0 of ggalt using the annotation_ticks layer.

这是在使用annotation_ticks层的ggalt版本0.6.0中实现的。

As of April 2018, ggalt 0.6.0 in not yet published on CRAN so you need to install development version:

截至2018年4月,ggalt 0.6.0尚未在CRAN上发布,因此您需要安装开发版本:

devtools::install_github("hrbrmstr/ggalt")

Here is an example:

这是一个例子:

library(ggplot2)
library(ggalt)

set.seed(20180407)

df = data.frame(x = seq(from = 1, by = 1, length.out = 20),
                y = 2^(seq(to = 1, by = -1, length.out = 20) + rnorm(20, 0, 0.7)))

ggplot(data = df, aes(x = x, y = y)) +
  geom_line() +
  scale_y_log10() +
  annotation_ticks(sides = "l", scale = c("log10")) 

R plot()或ggplot2()的对数y轴刻度标记

You can make it look even more than the paper you linked to with some theming:

你可以让它看起来甚至比你与一些主题链接的文章更重要:

ggplot(data = df, aes(x = x, y = y)) +
  geom_line(colour = "blue") +
  geom_point(colour = "blue") +
  scale_y_log10() +
  annotation_ticks(sides = "lb", scale = c("log10", "identity")) +
  theme_minimal() +
  theme(panel.grid = element_blank(), axis.line = element_line())

R plot()或ggplot2()的对数y轴刻度标记

#1


11  

In base R just build the axes however you want. Something like this could be a start.

在基底R中,只要建立坐标轴。像这样的事情可能是一个开始。

set.seed(5)
d <- data.frame(x=1:100, y=rlnorm(100, meanlog=5, sdlog=3))
with(d, {
  plot(x, y, log="y", yaxt="n")
  y1 <- floor(log10(range(y)))
  pow <- seq(y1[1], y1[2]+1)
  ticksat <- as.vector(sapply(pow, function(p) (1:10)*10^p))
  axis(2, 10^pow)
  axis(2, ticksat, labels=NA, tcl=-0.25, lwd=0, lwd.ticks=1)
})

In lattice, the latticeExtra package has the capability:

在格网中,latticeExtra包具有以下功能:

library(lattice)
library(latticeExtra)
xyplot(y~x, data=d, scales=list(y=list(log=10)),
       yscale.components=yscale.components.log10ticks)

#2


6  

For ggplot2, it seems that the only option you have for specifying ticks is the size (i.e., width).

对于ggplot2来说,指定刻度的惟一选项是大小(也就是)。、宽度)。

# A plot of any old data
dfr <- data.frame(x = 1:100, y = rlnorm(100))
p <- ggplot(dfr, aes(x, y)) + 
  geom_point() +
  scale_y_log10(breaks = breaks, labels = breaks)


#Tick locations
get_breaks <- function(x)
{
  lo <- floor(log10(min(x, na.rm = TRUE)))
  hi <- ceiling(log10(max(x, na.rm = TRUE)))
  as.vector(10 ^ (lo:hi) %o% 1:9)
}

breaks <- get_breaks(dfr$y)
log10_breaks <- log10(breaks)

#Some bigger ticks
p + opts(axis.ticks = theme_segment(
    size = ifelse(log10_breaks == floor(log10_breaks), 2, 1)
  ))

#3


3  

This has been done in package::sfsmisc. See the example in help(axTexpr)

这是在package::sfsmisc中完成的。参见help(axTexpr)示例

#4


0  

This is implemented in version 0.6.0 of ggalt using the annotation_ticks layer.

这是在使用annotation_ticks层的ggalt版本0.6.0中实现的。

As of April 2018, ggalt 0.6.0 in not yet published on CRAN so you need to install development version:

截至2018年4月,ggalt 0.6.0尚未在CRAN上发布,因此您需要安装开发版本:

devtools::install_github("hrbrmstr/ggalt")

Here is an example:

这是一个例子:

library(ggplot2)
library(ggalt)

set.seed(20180407)

df = data.frame(x = seq(from = 1, by = 1, length.out = 20),
                y = 2^(seq(to = 1, by = -1, length.out = 20) + rnorm(20, 0, 0.7)))

ggplot(data = df, aes(x = x, y = y)) +
  geom_line() +
  scale_y_log10() +
  annotation_ticks(sides = "l", scale = c("log10")) 

R plot()或ggplot2()的对数y轴刻度标记

You can make it look even more than the paper you linked to with some theming:

你可以让它看起来甚至比你与一些主题链接的文章更重要:

ggplot(data = df, aes(x = x, y = y)) +
  geom_line(colour = "blue") +
  geom_point(colour = "blue") +
  scale_y_log10() +
  annotation_ticks(sides = "lb", scale = c("log10", "identity")) +
  theme_minimal() +
  theme(panel.grid = element_blank(), axis.line = element_line())

R plot()或ggplot2()的对数y轴刻度标记