-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using a different plot_call in geom_node_plot #36
Comments
Thanks for using ggparty and helping finding issues! So, this would seem like a case for which ggparty's argument library("survival")
library("model4you")
#> Loading required package: partykit
#> Loading required package: grid
#> Loading required package: libcoin
#> Loading required package: mvtnorm
library("ggparty")
#> Loading required package: ggplot2
data(GBSG2, package = "TH.data")
## model
bmod <- survreg(Surv(time, cens) ~ horTh, data = GBSG2, model = TRUE)
surv_gg <- survreg_plot(bmod, data = cbind(GBSG2, id = 1))
## no id column
head(surv_gg$data)
#> horTh pr probability
#> 1 no 56.22822 0.99
#> 2 no 133.23025 0.97
#> 3 no 199.84924 0.95
#> 4 no 261.78897 0.93
#> 5 no 320.98908 0.91
#> 6 no 378.42847 0.89 Created on 2019-06-20 by the reprex package (v0.3.0) So this seems to be a a problem of this approach that's not so easy to work around. Nevertheless with the current version of geom_node_plot it's not possible to specify futher arguments for the new However, to get back to your problem: So although this seems like we could use tree <- pmtree(bmod)
#> No data given. I'm using data set GBSG2 from the current environment parent.frame(). Please check if that is what you want.
#get data for geom_node_plot's gglist
survplot_data <- vector("list", length(tree))
for (i in seq_along(tree)) {
gg <- survreg_plot(tree[i]$info$model)
survplot_data[[i]] <- cbind(gg$data, id = i)
}
survplot_data <- do.call(rbind, survplot_data)
# plot basis
p <- ggparty(tree) +
geom_edge() +
geom_edge_label() +
geom_node_label(aes(label = splitvar),
ids = "inner")
p +
geom_node_plot(gglist = list(geom_line(data = survplot_data,
mapping = aes(x = pr,
y = probability,
colour = horTh)
))) Created on 2019-06-20 by the reprex package (v0.3.0) So this is basically how it could work. As you can see, it doesn't really. This is due to survreg_plot(tree[1]$node$info$object)
#> Error in eval(modcall$data): object 'di' not found Created on 2019-06-20 by the reprex package (v0.3.0) |
Wow thanks for the super quick and useful help! Got it now: library("survival")
library("model4you")
library("ggparty")
library("plyr")
data(GBSG2, package = "TH.data")
## model
bmod <- survreg(Surv(time, cens) ~ horTh, data = GBSG2, model = TRUE)
tree <- pmtree(bmod)
# get data for geom_node_plot's gglist
obs_nodes <- predict(tree, type = "node")
get_plot_data <- function(i, data) {
dat <- subset(data, obs_nodes == i)
imod <- update(bmod, data = dat)
gg <- survreg_plot(imod, data = dat)
cbind(gg$data, id = i)
}
survplot_data <- ldply(unique(obs_nodes), .fun = get_plot_data, data = GBSG2)
# plot
p <- ggparty(tree) +
geom_edge() +
geom_edge_label() +
geom_node_label(aes(label = splitvar),
ids = "inner")
p +
geom_node_plot(gglist = list(geom_line(data = survplot_data,
mapping = aes(x = pr,
y = probability,
colour = horTh)
), xlim(0, 2900), xlab("time"),
theme_classic())) Created on 2019-06-21 by the reprex package (v0.2.1) Great work! Looks beautiful. |
great, I'm glad you worked it out! |
I'd like to use a custom function in
geom_node_plot
which takes the model as argument. Is this possible? If so, can you help me with the example below? Thanks in advance 🌻Created on 2019-06-20 by the reprex package (v0.2.1)
The text was updated successfully, but these errors were encountered: