Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R上使用ggplot2生成紧凑的科学记数法_R_Ggplot2_Axis Labels - Fatal编程技术网

在R上使用ggplot2生成紧凑的科学记数法

在R上使用ggplot2生成紧凑的科学记数法,r,ggplot2,axis-labels,R,Ggplot2,Axis Labels,我用ggplot2生成的图看起来像左边的图,y轴的每个刻度上都有完整的科学符号。我怎样才能让它看起来更紧凑,就像右边的情节一样,角落里用红色圈出了科学符号 我没有在ggplot2包文档或堆栈溢出中看到这一点。有人有解决办法吗 从类似的绘图开始: ggplot(mtcars, aes(wt, mpg * 1E-8)) + geom_point() 如果我们知道要使用的比例,我们可以定义它,然后我们可以在输入的过程中缩放数据,或者更改y轴上的标签,除了y轴标签(我们可以根据需要重命名它)外,

我用ggplot2生成的图看起来像左边的图,y轴的每个刻度上都有完整的科学符号。我怎样才能让它看起来更紧凑,就像右边的情节一样,角落里用红色圈出了科学符号

我没有在ggplot2包文档或堆栈溢出中看到这一点。有人有解决办法吗


从类似的绘图开始:

ggplot(mtcars, aes(wt, mpg * 1E-8)) +
  geom_point()

如果我们知道要使用的比例,我们可以定义它,然后我们可以在输入的过程中缩放数据,或者更改y轴上的标签,除了y轴标签(我们可以根据需要重命名它)外,其他看起来都一样:


编辑:

如果您还需要一个标题,也可以使用
annotate
在绘图区域外写入文本,然后将标题向上移动:

ggplot(mtcars, aes(wt, mpg * 1E-8)) +
  geom_point() +
  scale_y_continuous(labels = function(x) x / divisor) +
  annotate("text", x = -Inf, y = Inf, hjust = 0, vjust = -0.5,
           label = formatC(divisor, format = "e", digits = 0)) +
  coord_cartesian(clip = "off") +
  labs(title = "Title here") +
  theme(plot.title = element_text(margin = margin(0,0,20,0)))

感谢您对Jon Spring的详细回复。这是否也允许我放置一个标题?看起来除数取代了它。添加了另一种方法,可以保持添加单独标题的能力。
ggplot(mtcars, aes(wt, mpg * 1E-8)) +
  geom_point() +
  scale_y_continuous(labels = function(x) x / divisor) +
  labs(title = formatC(divisor, format = "e", digits = 0))
ggplot(mtcars, aes(wt, mpg * 1E-8)) +
  geom_point() +
  scale_y_continuous(labels = function(x) x / divisor) +
  annotate("text", x = -Inf, y = Inf, hjust = 0, vjust = -0.5,
           label = formatC(divisor, format = "e", digits = 0)) +
  coord_cartesian(clip = "off") +
  labs(title = "Title here") +
  theme(plot.title = element_text(margin = margin(0,0,20,0)))