There's support for this in the upcoming (currently development)
version of ggplot2 (0.9.0), where geom_path() has a new argument
lineend:
lineend Line end style (round, butt, square)
You could wait for the new version (should be soon but no official ETA
yet), or you could install the development version of 0.9.0 as
follows:
library('devtools') # install from CRAN first if necessary
dev_mode()
install_github('ggplot2') # current development version
library('ggplot2')
<your code>
Caution: there are a number of new functions and features, some of
which may break old code. Alternatively, the new code may help you
figure out how to modify the function(s) of interest in 0.8.9.
Dennis
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
If you could provide a full reproducible example (i.e. the code you
used with geom_segment2), I'd be more able to take a look quickly.
Please post back to the ggplot2 mailing list.
Hadley
On Wed, Jan 18, 2012 at 5:25 PM, James Cheshire
<james.c...@ucl.ac.uk> wrote:
> Dear Hadley,
>
> Do you have any thoughts on the problem below? I plan to integrate the
> solution into a ggplot2 tutorial I am creating for my research blog.
>
> Thanks in advance!
>
> James
>
> --
> Dr James Cheshire
> Lecturer in Advanced Spatial Analysis
> UCL Centre for Advanced Spatial Analysis
> 1st Floor 90 Tottenham Court Road, London, W1T 4TJ.
> spatialanalysis.co.uk
> +44 (0) 20 3108 3914
>
>
> ---------- Forwarded message ----------
> From: Dennis Murphy <djm...@gmail.com>
> Date: 18 January 2012 22:36
> Subject: Re: Change lineend for geom_segment()
> To: James Cheshire <jache...@gmail.com>
>
>
> Hi:
>
> That's always the rub when dealing with a layered, complex package
> like ggplot2. I was hoping that the new code might be translatable to
> geom_segment(), but was afraid that something would get in the way.
> Alas, I'm not intimate with the inner workings of ggplot2, so I can't
> help much there. Sorry.
>
> Dennis
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
library(ggplot2)
library(proto)
GeomSegment2 <- proto(ggplot2:::GeomSegment, {
objname <- "geom_segment2"
draw <- function(., data, scales, coordinates, arrow=NULL, ...) {
if (is.linear(coordinates)) {
return(with(coord_transform(coordinates, data, scales),
segmentsGrob(x, y, xend, yend, default.units="native",
gp = gpar(col=alpha(colour, alpha), lwd=size * .pt,
lty=linetype, lineend = "round"),
arrow = arrow)
))
}
}})
geom_segment2 <- function(mapping = NULL, data = NULL, stat =
"identity", position = "identity", arrow = NULL, ...) {
GeomSegment2$new(mapping = mapping, data = data, stat = stat,
position = position, arrow = arrow, ...)
}
df <- data.frame(x = 1, y = 1)
ggplot(df, aes(x , y)) + geom_segment2(aes(xend = x + 1, yend = y +
1), size = 4)
ggplot(df, aes(x , y)) + geom_segment(aes(xend = x + 1, yend = y + 1), size = 4)
I think the main problem with your code was that you were inheriting
from Geom instead of GeomSegment, and so your custom geom was missing
many important functions.
Hadley