Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:40:44+00:00 2026-06-09T20:40:44+00:00

I’m using the party package in R to create a decision tree with some

  • 0

I’m using the party package in R to create a decision tree with some data. My dependent variable has some long strings and so the horizontal presentation of the x-axis labels means that all levels of my dependent variable are not showing.

Using the survey data from the MASS package as a trivial example, with the party package loaded:

 data(survey)
 #remove missing value
 survey2 <- subset(survey, !is.na(Clap))
 TreeResults <- ctree(Clap ~ Sex + Exer + Smoke, data=survey2)
 plot(TreeResults)

The labels I wish to rotate are for Clap, and I would like these to print at 45 degrees for each node. My real data has two terminal nodes, just like this example.

I have looked at node_barplot and I cannot see an option within there to modify the orientation of the x-axis labels.

Could someone advise how to do the label reorientation?

Thanks in advance.

Update: printing the labels up the sides as per the suggestion produced this plot:
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:40:46+00:00Added an answer on June 9, 2026 at 8:40 pm

    Did you want something like?

    plot(TreeResults,tp_args = list(beside=F))
    

    UPDATE OP indicated this was not appropriate.

    Unfortunately I cant see an easy fix. You can redefine the node_barplot() function as follows:

    altbp<-function (ctreeobj, col = "black", fill = NULL, beside = NULL, 
        ymax = NULL, ylines = NULL, widths = 1, gap = NULL, reverse = NULL, 
        id = TRUE,rot=45) 
    {
        getMaxPred <- function(x) {
            mp <- max(x$prediction)
            mpl <- ifelse(x$terminal, 0, getMaxPred(x$left))
            mpr <- ifelse(x$terminal, 0, getMaxPred(x$right))
            return(max(c(mp, mpl, mpr)))
        }
        y <- response(ctreeobj)[[1]]
        if (is.factor(y) || class(y) == "was_ordered") {
            ylevels <- levels(y)
            if (is.null(beside)) 
                beside <- if (length(ylevels) < 3) 
                    FALSE
                else TRUE
            if (is.null(ymax)) 
                ymax <- if (beside) 
                    1.1
                else 1
            if (is.null(gap)) 
                gap <- if (beside) 
                    0.1
                else 0
        }
        else {
            if (is.null(beside)) 
                beside <- FALSE
            if (is.null(ymax)) 
                ymax <- getMaxPred(ctreeobj@tree) * 1.1
            ylevels <- seq(along = ctreeobj@tree$prediction)
            if (length(ylevels) < 2) 
                ylevels <- ""
            if (is.null(gap)) 
                gap <- 1
        }
        if (is.null(reverse)) 
            reverse <- !beside
        if (is.null(fill)) 
            fill <- gray.colors(length(ylevels))
        if (is.null(ylines)) 
            ylines <- if (beside) 
                c(3, 2)
            else c(1.5, 2.5)
        rval <- function(node) {
            pred <- node$prediction
            if (reverse) {
                pred <- rev(pred)
                ylevels <- rev(ylevels)
            }
            np <- length(pred)
            nc <- if (beside) 
                np
            else 1
            fill <- rep(fill, length.out = np)
            widths <- rep(widths, length.out = nc)
            col <- rep(col, length.out = nc)
            ylines <- rep(ylines, length.out = 2)
            gap <- gap * sum(widths)
            yscale <- c(0, ymax)
            xscale <- c(0, sum(widths) + (nc + 1) * gap)
            top_vp <- viewport(layout = grid.layout(nrow = 2, ncol = 3, 
                widths = unit(c(ylines[1], 1, ylines[2]), c("lines", 
                    "null", "lines")), heights = unit(c(1, 1), c("lines", 
                    "null"))), width = unit(1, "npc"), height = unit(1, 
                "npc") - unit(2, "lines"), name = paste("node_barplot", 
                node$nodeID, sep = ""))
            pushViewport(top_vp)
            grid.rect(gp = gpar(fill = "white", col = 0))
            top <- viewport(layout.pos.col = 2, layout.pos.row = 1)
            pushViewport(top)
            mainlab <- paste(ifelse(id, paste("Node", node$nodeID, 
                "(n = "), "n = "), sum(node$weights), ifelse(id, 
                ")", ""), sep = "")
            grid.text(mainlab)
            popViewport()
            plot <- viewport(layout.pos.col = 2, layout.pos.row = 2, 
                xscale = xscale, yscale = yscale, name = paste("node_barplot", 
                    node$nodeID, "plot", sep = ""))
            pushViewport(plot)
            if (beside) {
                xcenter <- cumsum(widths + gap) - widths/2
                for (i in 1:np) {
                    grid.rect(x = xcenter[i], y = 0, height = pred[i], 
                      width = widths[i], just = c("center", "bottom"), 
                      default.units = "native", gp = gpar(col = col[i], 
                        fill = fill[i]))
                }
                if (length(xcenter) > 1) 
                    grid.xaxis(at = xcenter, label = FALSE)
                grid.text(ylevels, x = xcenter, y = unit(-1, "lines"), 
                    just = c("center", "top"), default.units = "native", 
                    check.overlap = TRUE,rot=rot)
                grid.yaxis()
            }
            else {
                ycenter <- cumsum(pred) - pred
                for (i in 1:np) {
                    grid.rect(x = xscale[2]/2, y = ycenter[i], height = min(pred[i], 
                      ymax - ycenter[i]), width = widths[1], just = c("center", 
                      "bottom"), default.units = "native", gp = gpar(col = col[i], 
                      fill = fill[i]))
                }
                if (np > 1) {
                    grid.text(ylevels[1], x = unit(-1, "lines"), 
                      y = 0, just = c("left", "center"), rot = 90, 
                      default.units = "native", check.overlap = TRUE)
                    grid.text(ylevels[np], x = unit(-1, "lines"), 
                      y = ymax, just = c("right", "center"), rot = 90, 
                      default.units = "native", check.overlap = TRUE)
                }
                if (np > 2) {
                    grid.text(ylevels[-c(1, np)], x = unit(-1, "lines"), 
                      y = ycenter[-c(1, np)], just = "center", rot = 90, 
                      default.units = "native", check.overlap = TRUE)
                }
                grid.yaxis(main = FALSE)
            }
            grid.rect(gp = gpar(fill = "transparent"))
            upViewport(2)
        }
        return(rval)
    }
    

    all this did was add rot= to the grid.text call when beside=T
    then call the following:

    plot(TreeResults,terminal_panel=altbp(TreeResults,ylines=1.2, gap=0.05,rot=45))
    

    enter image description here

    SUMMARY:

    you can hack the node_barplot function a painless way to do it would be as follows:

    altbp<-node_barplot
    body(altbp)[[8]][[3]][[3]][[23]][[3]][[5]]<-substitute(
    grid.text(ylevels, x = xcenter, y = unit(-1, "lines"), just = c("center", 
        "top"), default.units = "native", check.overlap = TRUE,rot=45)
    )
    plot(TreeResults,terminal_panel=altbp(TreeResults))
    

    and use the altbp which will rotate text by 45 degrees.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.