在ggplot2中访问现有绘图的轴刻度矢量 [英] Accessing vector of axis ticks for an existing plot in ggplot2

查看:184
本文介绍了在ggplot2中访问现有绘图的轴刻度矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ggplot的现有图中获取坐标轴刻度值的向量。我知道ggplot对象是一个包含9个元素的列表,我想知道是否可以从该列表中提取轴蜱的值。例如,如果我生产这个玩具的例子:

  library(ggplot2)
g = ggplot(data = mtcars,aes( hp,mpg))+ geom_point()

我想要的是矢量

  c(100,200,300)
c(10,15,20,25,30,35)

分别用于x ticks和y ticks w



有没有办法去做这件事?

非常感谢!

解决方案

这些值可以在

  ggplot_build(g)$ panel $ ranges [[1]] $ x.major_source 
#[1] 100 200 300

  ggplot_build(g)$ panel $ ranges [[1] ] $ y.major_source 
#[1] 10 15 20 25 30 35



  ggplot_build(g)$ panel $ ranges [[1]] $ x.labels 
#[1]100 200300
ggplot_build(g)$ panel $ ranges [[1]] $ y.labels
#[1]1015202530 35


I would like to get the vector of values for axis ticks in an existing plot in ggplot. I know that a ggplot object is a list with 9 elements and I was wondering if somehow I could extract the values for the axis ticks from that list. For example if I produce this toy example:

library(ggplot2)
g=ggplot(data=mtcars,aes(hp,mpg))+geom_point()

What I want are the vectors

 c(100,200,300)
 c(10,15,20,25,30,35)

for x ticks and y ticks respectively w

Is there a way to to this?

Many thanks!

解决方案

Those values can be found in

ggplot_build(g)$panel$ranges[[1]]$x.major_source
#[1] 100 200 300

and

ggplot_build(g)$panel$ranges[[1]]$y.major_source
#[1] 10 15 20 25 30 35

They can also be found stored as characters here:

ggplot_build(g)$panel$ranges[[1]]$x.labels
#[1] "100" "200" "300"
ggplot_build(g)$panel$ranges[[1]]$y.labels
#[1] "10" "15" "20" "25" "30" "35"

这篇关于在ggplot2中访问现有绘图的轴刻度矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆