label position

94 views
Skip to first unread message

Hum

unread,
Jan 22, 2013, 8:38:30 PM1/22/13
to ggp...@googlegroups.com
I want to insert labels.
I have tried this;

geom_text(aes(label=size),data=d, size=3,hjust=0, vjust=0, angle=0)

this helps me to place the labels but I cannot position them like in this picture. I want labels at the end of the bar, like shown in this picture, pls. help.



Ben Bond-Lamberty

unread,
Jan 22, 2013, 8:53:51 PM1/22/13
to ggp...@googlegroups.com
...no picture attached. What do you want to do?





--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Sania

unread,
Jan 22, 2013, 9:39:07 PM1/22/13
to ggp...@googlegroups.com
I have attached the picture, also pasted below, I want to position labels at the end of the error bar.
label_position.bmp

Ben Bond-Lamberty

unread,
Jan 23, 2013, 4:58:21 AM1/23/13
to ggp...@googlegroups.com
Well, you know how wide the error bars are, so can compute a position for the labels. For example:

# Sample data set
d <- structure(list(x = c(1, 2, 3), y = c(1, 2, 3), xwidth = c(1, 2, 3), lab = structure(c(1L, 3L, 2L), .Label = c("One", "Three", "Two"), class = "factor")), .Names = c("x", "y", "xwidth", "lab"), row.names = c(NA, -3L), class = "data.frame")

ggplot(d,aes(x,y))+geom_point()+geom_errorbarh(aes(xmin=x-xwidth,xmax=x+xwidth))+geom_text(aes(x=x+xwidth,label=lab),hjust=0)

Ben


Sania

unread,
Jan 24, 2013, 5:12:41 PM1/24/13
to ggp...@googlegroups.com
Hi Ben,

Thank you very much. I have tried this on my own data, but doesn't fit well, I think something else is wrong, I have pasted below a reproducible example (have tried to attach it but can't), if you please help to fix it. I am just learning through hit and trial so don't understand any basics.

one more thing, I want to rearrange the y-axis tick labels, in the order I have written in the data sheet, while they are arranged in an alphabetical order.

can I split this graph according to the data given in column with heading "categ", and I want to use the text data in this column as graph titles, I have attached a sample graph, how I want to split it.

Sania

library( ggplot2 )
library( grid )


d <- structure(list(categ = structure(c(5L, 1L, 1L, 1L, 6L, 6L, 6L,
                                   4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
                                   3L, 3L), .Label = c("N number", "N rate", "Nproduct", "Ntime",
                                                       "Overall", "Ph_form"), class = "factor"), x = structure(c(7L,
                                                                                                                 4L, 5L, 6L, 14L, 2L, 3L, 8L, 9L, 10L, 1L, 15L, 13L, 12L, 11L,
                                                                                                                 16L, 20L, 19L, 17L, 25L, 23L, 18L, 22L, 24L, 21L), .Label = c("2post-join",
                                                                                                                                                                               "Liquid", "Liquid,solid", "N1", "N2", "N3", "Overall", "Post-join",
                                                                                                                                                                               "Pre-join", "Pre-post-join", "R high", "R low", "R med", "Solid",
                                                                                                                                                                               "Sow,join,boot", "U", "U+ni", "U+ni,u", "U+ui", "Uan", "Uan,u",
                                                                                                                                                                               "Uan,u+ui", "Uan+ni", "Uan+ni,u", "Uan+ui"), class = "factor"),
               size = c(588L, 515L, 67L, 6L, 396L, 186L, 6L, 238L, 277L,
                        58L, 9L, 6L, 385L, 120L, 83L, 209L, 131L, 100L, 85L, 25L,
                        26L, 2L, 4L, 2L, 4L), y = c(9, 8.9, 10.3, 7.6, 9.4, 8.2,
                                                    14.1, 7.3, 10.3, 11, 7.1, 7.6, 8.7, 7.1, 13.3, 8.8, 8.4,
                                                    11.3, 8.5, 4.1, 10.8, 11.5, 12.8, 12.2, 15.1), ylo = c(8,
                                                                                                           7.8, 7.7, 1.3, 8.3, 6.2, 5.6, 5.8, 8.8, 8.2, -0.9, 1.6, 7.5,
                                                                                                           4.9, 10.5, 7.2, 6.1, 9, 6.8, -3.2, 6.8, 1.4, 5.1, 0.9, 7.9
                                                    ), yhi = c(10, 10, 13.1, 12.1, 10.5, 10.2, 24.5, 8.8, 11.8,
                                                               13.9, 15.8, 12.1, 9.8, 9.1, 16.5, 10.5, 10.7, 13.7, 10.4,
                                                               10.8, 15.3, 26.4, 24.6, 29.9, 26), ywid = c(1, 1.1, 2.7,
                                                                                                           5.4, 1.1, 2, 9.45, 1.5, 1.5, 2.85, 8.35, 5.25, 1.15, 2.1,
                                                                                                           3, 1.65, 2.3, 2.35, 1.8, 7, 4.25, 12.5, 9.75, 14.5, 9.05)), .Names = c("categ",
                                                                                                                                                                                  "x", "size", "y", "ylo", "yhi", "ywid"), class = "data.frame", row.names = c(NA,
                                                                                                                                                                                                                                                               -25L))

#forest plot
forestplot <- function(d, xlab="Changes from the control %", ylab=""){
  require(ggplot2)
  p <- ggplot(d, aes(x=x, y=y, ymin=ylo, ymax=yhi, group=categ)) +
    coord_flip()+
    geom_point(size=3) + geom_errorbar(width=0)+
    geom_hline(aes(x=0), lty=2) +
    geom_text(aes(y=y+ywid, label=size), size=3,hjust=0, vjust=0, angle=0, position="dodge")+
    ylab(xlab) +
    xlab(ylab)+ #switch because of the coord_flip() above
    theme_bw()
  #facet_wrap(~ categ,nrow=10, ncol=1, scales = "free_x")
  return(p)
}
forestplot(d)
samplegraph.bmp

Dennis Murphy

unread,
Jan 24, 2013, 5:39:28 PM1/24/13
to Sania, ggp...@googlegroups.com
Hi:

You can order the levels of x as follows:

d$x <- factor(d$x, levels = d$x)

I also think you could improve the positioning of the labels as follows (the change occurs in the geom_text() line):

forestplot2 <- function(d, ylab="Changes from the control %", xlab=""){
  require(ggplot2)
  print( ggplot(d, aes(x=x, y=y, ymin=ylo, ymax=yhi, group=categ)) +
                theme_bw() +
                labs(x = xlab, y = ylab) +
                geom_point(size=3) + geom_errorbar(width=0) +
                geom_hline(aes(x=0), lty=2) +
                geom_text(aes(y= yhi + 1, label=size), size=3) +
                coord_flip()
       )
  #facet_wrap(~ categ,nrow=10, ncol=1, scales = "free_x")
  invisible()
}

forestplot2(d)

Dennis

--

Ben Bond-Lamberty

unread,
Jan 24, 2013, 9:08:47 PM1/24/13
to ggplot2
Nice, Dennis. Sania, note that the "facet_wrap" is commented out in this solution, but that's what you'll want to use to segment this graph into panels, if that's important to you.

Ben

Sania

unread,
Jan 25, 2013, 5:39:55 PM1/25/13
to ggp...@googlegroups.com
Thank you very much.

Sania

Sania

unread,
Jan 26, 2013, 12:09:18 AM1/26/13
to ggp...@googlegroups.com, Sania
Hi,

in the same code, set by Dennis, if I want to use the command


facet_wrap(~ categ,nrow=10, ncol=1, scales = "free_x")

it gives following error,

Error in facet_render.wrap(plot$facet, panel, plot$coordinates, plot_theme(plot),  : 
  ggplot2 does not currently support free scales with a non-cartesian coord or coord_flip.

How to resolve it?

thanks

Sania

Ben Bond-Lamberty

unread,
Jan 27, 2013, 7:12:22 AM1/27/13
to ggp...@googlegroups.com
I would rewrite Dennis's code so it doesn't use coord_flip:

p <- ( ggplot(d, aes(x=y, y=x, xmin=ylo, xmax=yhi, group=categ)) +
                 theme_bw() +
                 labs(y = xlab, x = ylab) +
                 geom_point(size=3) + geom_errorbarh(width=0,height=0) +
                 geom_line(aes(x=0), lty=2) +
                 geom_text(aes(x= yhi + 1, label=size), size=3)
 )

and then do

p + facet_grid(categ~.,scales = "free_y")

Ben
Reply all
Reply to author
Forward
0 new messages