Skip to content

ggplot2 geom_polygon fill

2 messages · raimund, Hadley Wickham

#
Hi everyone,

i've been trying to make a special plot with ggplot2, but I can't get it to
fill the polygon I'd like to see filled so very very much.

I want to display the difference or change in the distribution of the
modified Rankin Scale between two groups. mRS is a scale for disability or
daily activities competence.

It looks like this.

http://r.789695.n4.nabble.com/file/n4361919/rankinplot.png 

I just wish the polygons in between the bars would fill in the same colors
as the bar segments do.
Interestingly, in the example provided by the geom_polygon help page, there
is a fill, which leads me to suspect that something with my data frame might
be wrong.

If I supply a "colour" argument, I get borders, but not always in the color
I defined. The attached image has such borders, to make clear what polygons
I am talking about. In the final plot I don't desire such borders, only the
as of yet unattainable fill.


Here's my code:

library(ggplot2)
library(plyr)


# define good looks

no_margins <- opts(
  axis.line =         theme_blank(),
  axis.text.x =       theme_blank(),
  axis.ticks =        theme_blank(),
  axis.title.x = theme_text(size = 12, vjust = 0.15),
  axis.title.y = theme_text(angle = 90, size = 12, vjust = 0.2),
  axis.ticks.length = unit(0, "cm"),
  axis.ticks.margin = unit(0, "cm"),
  panel.background =  theme_blank(),
  panel.grid.major =  theme_blank(),
  panel.grid.minor =  theme_blank(),
  plot.background =   theme_blank(),
  plot.title =        theme_blank(),
  plot.margin =       unit(c(1, 1, 1, 1.5), "lines")
  )   

sfm = scale_fill_manual("mRS", c("0"="darkgreen",
                                 "1"="forestgreen",
                                 "2"="mediumseagreen",
                                 "3"="coral",
                                 "4"="red",
                                 "5"="darkred",
                                 "6"="black"))

barwidth = 0.6

# good looks defined

smalldummy = data.frame(
  mRS = as.factor(rep(0:6,2)),
  rsfreq = sample(0:6,14,replace=T),
  treatment = factor(rep(c("one","two"),each=7))
  )

smalldummy = ddply(smalldummy, .(treatment), transform,
                   textpos = cumsum(rsfreq/sum(rsfreq)) -
rsfreq/sum(rsfreq)/2, # center within segment
                   lineposx = cumsum(rsfreq/sum(rsfreq)))                      
# segment borders without 0



# make the xs of the polygon
polylo = 1 + barwidth/2
polyhi = 2 - barwidth/2
xs = rep(c(polylo,polyhi,polyhi,polylo), 7)

# make the ys of the polygon
tmp1 = c(0, smalldummy$lineposx[1:7])
tmp2 = c(0, smalldummy$lineposx[8:14])
ys = c()
for(i in 1:7) {
  nu = c(tmp1[i], tmp2[i], tmp2[i+1], tmp1[i+1])
  ys = c(ys, nu)
}
m = as.factor(rep(0:6, each=4))
tmpdf = data.frame(xs, ys, mRS = m)


bigdummy = merge(smalldummy, tmpdf, by = "mRS")

ggplot(data = bigdummy, aes(x = treatment, y = rsfreq, fill = mRS)) +
  geom_bar(aes(width = barwidth),stat="identity",position="fill") +
  geom_text(aes(label=as.character(mRS),
    y = ifelse(rsfreq != 0, textpos, NA)),
    size = 8, colour = "white") +
  geom_polygon(aes(x = xs, y = ys, group = mRS, fill = mRS)) +
  ylab("Modified Rankin Scale") + xlab("Treatment") +
  coord_flip() + theme_bw() + opts(legend.position = "none") + no_margins +
sfm




--
View this message in context: http://r.789695.n4.nabble.com/ggplot2-geom-polygon-fill-tp4361919p4361919.html
Sent from the R help mailing list archive at Nabble.com.
3 days later
#
Hi Raimund,

To increase your chances of getting help, I'd recommend using the
ggplot2 mailing list, and reducing your example down to the essence of
the problem.  For example, the theme components don't affect the
problem, but make the code longer, and so harder to understand.

Hadley
On Mon, Feb 6, 2012 at 10:50 AM, raimund <raipec at gmail.com> wrote: