ggplot 多行日志标签

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

我有一些数据正在绘制,如下所示:

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

看起来不错,但我希望轴上的数字看起来像这张图片中的那样: sphere drag coefficient

有人知道这个吗?

r ggplot2 visualization axis-labels
1个回答
0
投票
brks  <-  c(c(1,2,4,6,8) %o% 10^(0:10))
# using a trick from https://stackoverflow.com/a/23902261/6851825

labs <- if_else(log10(brks) == floor(log10(brks)),
                paste0("\n10^", log10(brks)),
                paste(brks / 10^floor(log10(brks))))

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

enter image description here

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