最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - ggplot multi-line log-log labels - Stack Overflow

programmeradmin1浏览0评论

I have some data I'm plotting like this:

g <- ggplot(datapile, aes(x = Re, y = CD))
g <- g + geom_line(data = datapile, linetype = "solid", size = 1)
g <- g + scale_y_continuous(trans='log10', limits = c(0.01, 400), n.breaks = 20)
g <- g + scale_x_continuous(trans='log10', limits = c(0.02, 1e7), n.breaks = 20)
g <- g + ylab(expression(Drag~Coefficient*","~C[D]))
g <- g + xlab(expression(Reynolds~Number*","~frac(rho~V~d, mu)))
g

It looks fine, but I'd like the numbers on the axis to appear like they do in this picture:

Anyone know this offhand?

I have some data I'm plotting like this:

g <- ggplot(datapile, aes(x = Re, y = CD))
g <- g + geom_line(data = datapile, linetype = "solid", size = 1)
g <- g + scale_y_continuous(trans='log10', limits = c(0.01, 400), n.breaks = 20)
g <- g + scale_x_continuous(trans='log10', limits = c(0.02, 1e7), n.breaks = 20)
g <- g + ylab(expression(Drag~Coefficient*","~C[D]))
g <- g + xlab(expression(Reynolds~Number*","~frac(rho~V~d, mu)))
g

It looks fine, but I'd like the numbers on the axis to appear like they do in this picture:

Anyone know this offhand?

Share Improve this question asked Nov 21, 2024 at 0:43 Karl WolfschtaggKarl Wolfschtagg 5672 silver badges16 bronze badges 1
  • You mean specifically the x-axis? (Seems tricky ...) – Ben Bolker Commented Nov 21, 2024 at 1:09
Add a comment  | 

1 Answer 1

Reset to default 5
library(ggplot2) 
library(ggtext) # to specify superscript etc. using html

# using a trick from https://stackoverflow/a/23902261/6851825
# "outer binary product" multiplies all combinations of the two vectors, 
#    concatenate to get vector of breaks
brks  <-  c(c(1,2,4,6,8) %o% 10^(0:10))
brks_log <- log10(brks)
labs <- ifelse(brks_log == floor(brks_log),
               paste0("<br>10<sup>", brks_log, "</sup>"),
               paste(brks / 10^floor(brks_log)))

ggplot(mtcars, aes(wt^5, mpg^3)) +
  geom_point() +
  scale_x_log10(breaks = brks, minor_breaks = NULL,
                labels = labs, guide = "axis_logticks") +
  theme(axis.text.x = element_markdown())

发布评论

评论列表(0)

  1. 暂无评论