我怎样才能改变轴'规模(或间隔)?

时间:2018-06-03 08:23:08

标签: r ggplot2

我想调整轴'比例(或间隔)。

另一方面,我遇到了一些麻烦。

这是我的代码,我已经实现了以下内容。

install.packages("randomForestSRC")
install.packages("ggRandomForests")

library(randomForestSRC)
library(ggRandomForests)

data(pbc, package="randomForestSRC") 
pbc.na <- na.omit(pbc)

set.seed(123) 
rsf <- rfsrc(Surv(days, status)~., data=pbc.na,     
             ntree=500, nplist=1, importance=T, proximity=T)

out.vs <- var.select (rsf)
gg_md <- gg_minimal_depth(out.vs)

在这种情况下,图表可以像这样显示。

enter image description here

但是,我想要做的是将x轴刻度从0调整到22为1.

因此,虽然我已经执行了下面添加的代码,但它没有用。

install.packages("tidyverse")
library(tidyverse)

ggplot2::ggplot(gg_md, ggplot2::aes(x=varselect$depth, y=reorder(md.obj$count, varselect$depth))) + 
  ggplot2::geom_point() +
  ggplot2::scale_x_continuous(breaks=1:22, labels=1:22) +
  ggplot2::geom_abline(slope=1, lty=2, color="red") +
  ggplot2::geom_hline(xintercept = attr(gg_md, "modelsize") + .5, lty=2, color="black")+
  ggplot2::theme_bw()

请让我知道如何做我想做的事。

永远感谢。

1 个答案:

答案 0 :(得分:1)

从运行代码开始,gg_md似乎是一个列表,而不是data.frame。 ggplot不知道如何阅读它。这就是我得到的错误:Error: ggplot2 doesn't know how to deal with data of class gg_minimal_depth/list

我正在使用您的示例数据。 请查看我对您的代码的调整。根据我的理解,这是你想要的(?)。

library(tidyverse)

 gg_md$varselect %>% 
  rownames_to_column("name") %>%
  ggplot(aes(x=name, y=depth)) + 
  geom_point() +
  coord_flip() + 
  scale_y_continuous(breaks=seq(0, 22, by = 1),
                     limits = c(0, 22)) +
  geom_abline(slope=1, lty=2, color="red") +
  geom_hline(yintercept = gg_md$modelsize + .5,
             lty=2, color="black") +
  theme_bw()

结果:

enter image description here