不希望显示来自ggplot2的次要刻度的所有标签

问题描述 投票:2回答:1

我搜索并在Python中发现了一个类似的帖子而不是R.我在这篇文章Logarithmic y-axis Tick Marks in R plot() or ggplot2()中使用了Richie Cotton的代码。我不想显示小标记的所有标签,我只想显示主要标记,例如1,10,100等。请参阅下面的示例图像,这就是为什么我不想显示所有次标记的标签。我试图在代码中删除“labels = breaks”enter image description here,但什么也没发生。

library(ggplot2)

dfr <- data.frame(x = 1:100, y = rlnorm(100))

p <- ggplot(dfr, aes(x, y)) + 
  geom_point() +
  scale_x_log10(breaks = breaks, labels = breaks)

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$x)

log10_breaks <- log10(breaks)

p + labs(axis.ticks = element_line(
  size = ifelse(log10_breaks == floor(log10_breaks), 2, 1)
  ))
r ggplot2
1个回答
4
投票

你可以做到这一点。

ggplot(dfr, aes(x, y)) + 
  geom_point() +
  scale_x_log10(breaks = breaks, labels = c(breaks[1:3], rep("", 24))) 

收益率:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.