首页 > 解决方案 > ggplot plot x 轴随 log10 问题转移

问题描述

我有很多 PDF 分发号码(附件)。因为正常的方式,我无法绘制它。然后我用 log10 传输 x 轴。但是,有没有办法仍然显示原始 x 轴(不取 log10)?

ggplot(data = dist_cdf,aes(x= log10(num), y=pert_sales)) + 
  theme(panel.background = element_rect(fill = "white", colour = "black") + 
  stat_smooth(method = lm, formula = y ~ poly(x, 9), se = FALSE,
              span = 1.5, size=1.2, colour = "#FF3300", linetype = 1)

目前的数字是这样的:

在此处输入图像描述

部分数据:</p>

structure(list(num = 1:30, pert_sales = c(0.020194064, 0.020140418, 
0.014049199, 0.012375386, 0.008335432, 0.007140572, 0.006361819, 
0.006179615, 0.005034322, 0.004976598, 0.004922225, 0.004602446, 
0.004490266, 0.004264869, 0.0039289, 0.00387972, 0.003612034, 
0.00357951, 0.00337985, 0.00326423, 0.003048265, 0.002862149, 
0.002769482, 0.002764383, 0.002760949, 0.002760627, 0.002721623, 
0.002617593, 0.002405228, 0.002319419)), row.names = c(NA, 30L
), class = "data.frame")

标签: rggplot2

解决方案


如果你想要一个 log10 x 轴,它比在 aes 中转换数字本身要好得多,scale_x_log10()就好像你添加其他 aes 或限制等它们也会被转换,也给你原始数据的标签而不是 log10。要停止这些以科学记数法(1e+01、1e+10 等),您可以使用标度库中的标签 = 逗号。所以:

library(scales)
ggplot(data = dist_cdf,aes(x= num, y=pert_sales)) + 
  theme(panel.background = element_rect(fill = "white", colour = "black")) + 
  scale_x_log10(labels = comma)+
          stat_smooth(method = lm, formula = y ~ poly(x, 9), se = FALSE,span = 1.5,size=1.2,colour = "#FF3300",linetype = 1)

推荐阅读