gpt4 book ai didi

r - ggplot : How to retrieve values for axis labels?

转载 作者:行者123 更新时间:2023-12-04 12:03:10 26 4
gpt4 key购买 nike

如何提取下面ggplot中分别用于标记y和x轴的数字(分别为20, 30, 4010 , 15 ,20 ,25, 30, 35)?

情节

r-statistics.co

enter image description here

可复制的代码

# 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"

编辑:
ggplot2_3.3.0开始,使用以下命令找到标签:

# check package version
utils::packageVersion("ggplot2")

y_labs <- ggplot_build(g)$layout$panel_params[[1]]$y$get_labels()
y_labs[!is.na(y_labs)]
#[1] "20" "30" "40"
x_labs <- ggplot_build(g)$layout$panel_params[[1]]$x$get_labels()
x_labs[!is.na(x_labs)]
#[1] "10" "15" "20" "25" "30" "35"

关于r - ggplot : How to retrieve values for axis labels?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51784640/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com