Plot a subtree from a big decision tree I am working on my thesis using decision trees. I am presenting the resulting tree to show how they help in exploring data. My issue is that since the tree is big, I want to break it down into parts, e.g. print the first 4 levels, then to go deeper. I am using the R package rpart, then plot.rpart(prp)). 
I tried coercing the rpart object to party, to print a subtree from a given node - but I did not manage to obtain a nice looking tree with no errors at the terminal nodes. I only want the predicted count to be visible and that the nodes don't overlap. 
How can I obtain a nice looking yet error free tree?
library(rpart)
library(rpart.plot)
library("partykit")

reg.tree<-rpart(formula,data=mydata.train,method="anova",cp=0.001)
#AK first coerce the rpart tree to party
pfit <- as.party(reg.tree)

#AK OR Another route is to subset the subtree starting from node 83 
#AK and then taking the data from that:
pfit40 <- pfit[83]

plot(pfit40, main = "subtree from node 31",type =  "simple",
    tp_args = list(),
    inner_panel = node_inner, 
    edge_panel = edge_simple, ep_args = list(),
    drop_terminal = NULL,
    tnex = NULL, pop = FALSE, gp = gpar(fontsize = 7))


 A: One obvious option is, of course, to make the plotting device larger so that all the nodes have enough space on the device. Another possible way to save space is to just show the prediction in the terminal nodes (without the (n = ..., err = ...) part. This is possible through the FUN argument to the node_terminal panel function. For example, you could try:
plot(as.simpleparty(pfit40),
  tp_args = list(FUN = function(info)
    format(round(info$prediction, digits = 1), nsmall = 1)
  )
)

The first line has a similar effect as type = "simple" that you used. However, it also enriches the info available for printing. The second and third line set up the function that computes the information for display in the terminal node. In my example, it just shows the predicted mean (rounded to 1 digit).
(Additional remark: In your particular case it might be worth to explore different specifications of MonthNr/Hour. Currently, these are apparently unordered factors which leads to many splits.)
A: Here's an approach using the nodeprune() functiion. BUT it insists on renumbering all the node_ids. Not always desirable!
# to find parents of nodes see "2.5. Methods and other utilities" in https://cran.r-project.org/web/packages/partykit/vignettes/partykit.pdf
#<--- get list of terminal node ids \ get list of all node ids \ get list of non-terminal node ids \
#      loop through non-terminal node-ids \ for each list out its terminal node ids \ if target node id is there, mark that node safe from pruning etc.
#<--- but when pruning off the node_ids not marked safe, do so in *reverse* numeric order so that we won't try to prune a node that is not there!
node_ids_leaves <- nodeids(fit_ctree, terminal = TRUE)
node_ids_all <- nodeids(fit_ctree)
node_ids_not_leaves<-node_ids_all [!(node_ids_all %in% node_ids_leaves)]
node_id_target <- 7  
df_prune<-NULL
for(node_id in node_ids_not_leaves) {
  df_prune<-rbind(data.frame(df_prune),  c(node_id, (
                                                    node_id_target %in% (nodeids(fit_ctree[node_id], terminal = TRUE)-1+node_id)) 
                                                    ) 
                  )
}
names(df_prune)<-c("node_id","prune_if_zero")
df_prune<-df_prune %>% filter(prune_if_zero==0) %>% arrange(desc(node_id))
fit_ctree_pruned<-fit_ctree
fit_ctree_pruned<-nodeprune(fit_ctree_pruned, df_prune$node_id)
### begin ### experiment to see if original node_ids are preserved as names ------------
nodeids(fit_ctree)
names(fit_ctree)
nodeids(fit_ctree_pruned)
names(fit_ctree_pruned)
# the original node names are not preserved!
### end ### experiment to see if original node_ids are preserved as names ------------
paste("node_id:",node_id_target)
plot(fit_ctree_pruned, main=paste("node_id:",node_id_target)
     , terminal_panel = node_barplot(obj=fit_ctree_pruned, id=FALSE , fill = c("blue","dark grey")      ) 
    )
# sadly, the node_ids in the plot are re-numbered. https://cran.r-project.org/web/packages/partykit/partykit.pdf

