当前位置 : 主页 > 网络推广 > seo >

在ggplot2_3.0.0中检索轴标签的值

来源:互联网 收集:自由互联 发布时间:2021-06-16
如何在下面的ggplot中提取用于标记y轴和x轴的数字(分别为20,30,40和10,15,20,25,30,35)? 情节 从r-statistics.co起 可重现的代码 # Scatterplottheme_set(theme_bw()) # pre-set the bw theme.g - ggplot(mpg, aes(cty, h
如何在下面的ggplot中提取用于标记y轴和x轴的数字(分别为20,30,40和10,15,20,25,30,35)?

情节

从r-statistics.co起

可重现的代码

# Scatterplot
theme_set(theme_bw())  # pre-set the bw theme.
g <- ggplot(mpg, aes(cty, hwy))
g + geom_count(col="tomato3", show.legend=F) +
  labs(subtitle="mpg: city vs highway mileage", 
       y="hwy", 
       x="cty", 
       title="Counts Plot")

我已经尝试查看str(g)的输出,但是成功了.

基于CPak的答案,ggplot2_3.0.0的结构略有变化.现在可以使用以下标签提取标签:

ggplot_build(g)$layout$panel_params[[1]]$y.labels
#[1] "20" "30" "40" 
ggplot_build(g)$layout$panel_params[[1]]$x.labels
#[1] "10" "15" "20" "25" "30" "35"
网友评论