Linetype with geom_polygon

1,218 views
Skip to first unread message

thomas

unread,
Oct 6, 2010, 10:11:31 AM10/6/10
to ggplot2
Hi,

I try to use linetype with geom_polygon without success...
Starting with the help example,

ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
values <- data.frame(id = ids, value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5))
positions <- data.frame(
id = rep(ids, each = 4),
x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,
0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,
2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)
)
datapoly <- merge(values, positions, by=c("id"))
qplot(x, y, data=datapoly, geom="polygon", fill=value, group=id)

I try to use

qplot(x, y, data=datapoly, geom="polygon", fill=value, group=id,
colour=I("black"), linetype=I(2))
qplot(x, y, data=datapoly, geom="polygon", fill=value, group=id,
colour=I("black"), linetype=id)

without success. Do I write anything wrong?

Thank you for your help...
Thomas

Brandon Hurr

unread,
Oct 6, 2010, 10:16:28 AM10/6/10
to thomas, ggplot2
Thomas,

I'm running a bunch of code so I can't check, but I don't think that
linetype and line colour can be used simultaneously. Try your code
without the colour=i("black") and see if it works.

Brandon

> --
> 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
>

Brandon Hurr

unread,
Oct 6, 2010, 10:29:45 AM10/6/10
to thomas, ggplot2
Thomas,

The default colour is NA according to Hadley's page.
http://had.co.nz/ggplot2/geom_polygon.html

This means that the border between area's won't be shown so selecting
a linetype won't make a difference because it won't be shown. After
playing, it seems that when you define a colour you see the line, but
then linetype fails to work because you've defined a colour.

There's probably a workaround, but I don't see it.

Brandon

takahashi kohske

unread,
Oct 6, 2010, 10:35:39 AM10/6/10
to Brandon Hurr, thomas, ggplot2
hi, probably this is a bug.
put this code before using goem_polygon

GeomPolygon$draw<- function (., data, scales, coordinates, ...)
{
n <- nrow(data)
if (n == 1)
return()
ggname(.$my_name(), gTree(children = gList(with(coordinates$munch(data,
scales), polygonGrob(x, y, default.units = "native",
gp = gpar(col = colour, fill = alpha(fill, alpha), lwd = size *
.pt, lty = linetype))))))
}

to Hadley:
the argument in gar in polygonGrob in gTree in ggname, linetype =
linetype should be lty = linetype.
as far as i inspected, this appears only in GeomPolygon$draw but not
in other goems.

Brandon Hurr

unread,
Oct 6, 2010, 10:44:09 AM10/6/10
to takahashi kohske, thomas, ggplot2
Takahashi,

GeomPolygon$draw<- function (., data, scales, coordinates, ...)
{
  n <- nrow(data)
  if (n == 1)
      return()
  ggname(.$my_name(), gTree(children = gList(with(coordinates$munch(data,
      scales), polygonGrob(x, y, default.units = "native",
      gp = gpar(col = colour, fill = alpha(fill, alpha), lwd = size *
          .pt, lty = linetype))))))
}

polygraph<-ggplot(data=datapoly, aes(x=x, y=y))+
polygraph+geom_polygon(aes(fill=value, group=id), size=2, linetype=2, colour="black")


Seems to work.

Brandon
polygraph.png

Mark Connolly

unread,
Oct 6, 2010, 11:02:48 AM10/6/10
to thomas, ggplot2
does geom_path help you out?

ggplot(utah.df) +aes(long,lat,group=group,fill=LEVEL3_NAM) +
geom_polygon() +
geom_path(color="white",linetype=2) +
coord_equal() +
scale_fill_brewer("Utah Ecoregion")

thomas

unread,
Oct 7, 2010, 8:50:38 AM10/7/10
to ggplot2
This fix my problem, with the example and with my real code.
Many thanks!

Thomas



On Oct 6, 4:35 pm, takahashi kohske <takahashi.koh...@gmail.com>
wrote:
> > On Wed, Oct 6, 2010 at 15:16, Brandon Hurr <bhiv...@gmail.com> wrote:
> >> Thomas,
>
> >> I'm running a bunch of code so I can't check, but I don't think that
> >> linetype and line colour can be used simultaneously. Try your code
> >> without the colour=i("black") and see if it works.
>
> >> Brandon
>

Hadley Wickham

unread,
Oct 12, 2010, 10:37:22 PM10/12/10
to takahashi kohske, Brandon Hurr, thomas, ggplot2
Thanks for the fix - it's now in the devel version.
Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages