+ Reply to Thread
Results 1 to 2 of 2

Thread: GGPLOT2 - Sub Groups

  1. #1
    Points: 21,235, Level: 91
    Level completed: 77%, Points required for next Level: 115

    Posts
    570
    Thanks
    51
    Thanked 20 Times in 19 Posts

    GGPLOT2 - Sub Groups




    Hi,

    I'm trying to create a stratified forest plot in ggplot2. In our analyses, we prepared a logistic regression model for each strata (Immigrants and Residents). I have included what I have (have.png) and what I need (need.png) in two images attached. Is there a simple way to do this in GGPLOT2?

    (I've foresee a way, through creating a duplicate "Group variable" entry and suppressing the text manually on the graph through theme options, however I'm wondering if there is an easier, more efficient way)

    Thanks in advance!

    Also the need.png was a hack and slash job in mspaint, don't judge me lol!

    Here are the data (from DPUT) and the GGPLOT2 Code:

    Code: 
     dats <- structure(list( group = structure(c(10L, 10L, 11L, 11L, 12L, 
    12L), .Label = c("t", "y", "u", 
    "a", "b", "c", "w", "e", "s", 
    "Males vs Females", "High vs Low", "New vs Old"), class = "factor"), 
         strata = structure(c(3L, 4L, 3L, 4L, 3L, 4L), .Label = c("a", 
        "b", "Immigrant", "Resident"), class = "factor"),  est = c(1, 
        1.45, 1.56, 1.03, 1.46, 1.55),  low = c(1, 1.4, 1.5, 
        1, 1.31, 1.49),  high = c(1, 1.5, 1.63, 1.07, 1.51, 1.61
        )), .Names = c("group", "strata", "est", "low", 
    "high"), row.names = c(NA, -6L), class = "data.frame")
    Code: 
    forest1 <- ggplot(dats, aes(x = group  , y = est, ymin=low, ymax=high)) +
                              geom_hline(aes(yintercept = 1),linetype = 2, size=0.5) +
    			  geom_errorbar(aes(ymin=low,ymax=high,color=strata), width=0.1 ) +
                              geom_pointrange(shape = 16,  aes(color=strata),  size = 0.75, stroke = 0.5) +
                              coord_flip() +
    			  theme_bw()
    Attached Thumbnails Attached Thumbnails Click image for larger version

Name:	have.jpg‎
Views:	46
Size:	8.4 KB
ID:	6706   Click image for larger version

Name:	need.jpg‎
Views:	40
Size:	8.4 KB
ID:	6707  
    Last edited by jamesmartinn; 07-14-2017 at 10:55 AM.

  2. #2
    Points: 944, Level: 16
    Level completed: 44%, Points required for next Level: 56

    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: GGPLOT2 - Sub Groups


    Took a bit of time for me to remember this but it's quite easy, you just need the position argument in geom_errorbar and geom_pointrange like
    Code: 
    ggplot(dats, aes(x = group, y = est, ymin=low, ymax=high, colour=strata, group=interaction(group, strata))) +
      geom_hline(aes(yintercept = 1), linetype = 2, size=0.5) +
      geom_errorbar(aes(ymin=low, ymax=high), width=0.1, position = position_dodge(width = 0.90)) +
      geom_pointrange(shape = 16,  size = 0.75, stroke = 0.5, position = position_dodge(width = 0.90)) +
      coord_flip() +
      theme_bw()
    Play around with the 'width' argument until you get what you're looking for.

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats