Rplot() 或 ggplot2() 中的对数 y 轴刻度线

发布于 2024-11-03 14:36:21 字数 206 浏览 5 评论 0 原文

我在 log="y" 图的理想刻度线结构>本文,图 3b 3c 3d。

它具有不带标签的短的、对数间隔的小刻度线,以及带标签的长的、对数间隔的主刻度线。

有谁知道如何在 R 中实现这一点?

I saw the ideal tick-mark structure for a log="y" plot in this paper, Figure 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暗地喜欢 2024-11-10 14:36:21

在基础 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)
})

lattice中,latticeExtra包具有以下功能:

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

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

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:

library(lattice)
library(latticeExtra)
xyplot(y~x, data=d, scales=list(y=list(log=10)),
       yscale.components=yscale.components.log10ticks)
记忆で 2024-11-10 14:36:21

对于 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)
  ))

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

# 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)
  ))
葮薆情 2024-11-10 14:36:21

这是在 package::sfsmisc 中完成的。请参阅帮助中的示例(axTexpr)

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

﹎☆浅夏丿初晴 2024-11-10 14:36:21

这是一个 ggplot2 解决方案:

library(ggplot2)

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_logticks(sides = "l") 

example_plot

您可以通过某些主题使其看起来比链接到的论文更丰富:

ggplot(data = df, aes(x = x, y = y)) +
  geom_line(colour = "blue") +
  geom_point(colour = "blue") +
  scale_y_log10() +
  annotation_logticks(sides = "l") +
  theme_minimal() +
  theme(panel.grid = element_blank(), 
        axis.line = element_line(),
        axis.ticks.x = element_line())

example_themed< /a>

Here is a ggplot2 solution:

library(ggplot2)

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_logticks(sides = "l") 

example_plot

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_logticks(sides = "l") +
  theme_minimal() +
  theme(panel.grid = element_blank(), 
        axis.line = element_line(),
        axis.ticks.x = element_line())

example_themed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文