yake84
yake84

Reputation: 3236

tearing when plotting polygons with holes in ggplot2

I'm trying to plot these body maps and can't get the face to show correctly. The "heads" are made up of two polygons: one is the face (solid oval) and the other is the rest of the head (donut-shaped, hole in the center). These should be colored based on the value of the "score" column. I've tried so many variations on where the group=group gets placed and the fill=scale.

comparison of real data

  ggplot(data=Patients, aes(y=lat, 
                            x=long, 
                            fill=factor(score), 
                            group=group))+
    geom_polygon(color="black")+
    facet_wrap(~Patient, 
               ncol = 5)+
    scale_fill_manual(values=scoreColor)+
    coord_fixed()

Any help would be appreciated.

I've included my attempt at a reproducible example below.

library(ggplot2)
fortifyResults <-read.table(header=T, stringsAsFactors = F, text="
                  long  lat order   hole    piece   group   id  score
                  1 1   1   FALSE   1   Outer.1 Outer   20
                  1 4   2   FALSE   1   Outer.1 Outer   20
                  4 4   3   FALSE   1   Outer.1 Outer   20
                  4 1   4   FALSE   1   Outer.1 Outer   20
                  1 1   5   FALSE   1   Outer.1 Outer   20
                  2 2   6   TRUE    2   Outer.2 Outer   20
                  3 2   7   TRUE    2   Outer.2 Outer   20
                  3 3   8   TRUE    2   Outer.2 Outer   20
                  2 3   9   TRUE    2   Outer.2 Outer   20
                  2 2   10  TRUE    2   Outer.2 Outer   20
                  2 2   11  FALSE   1   Inner.1 Inner   10
                  2 3   12  FALSE   1   Inner.1 Inner   10
                  3 3   13  FALSE   1   Inner.1 Inner   10
                  3 2   14  FALSE   1   Inner.1 Inner   10
                  2 2   15  FALSE   1   Inner.1 Inner   10
                  ")

This version of the code doesn't show both pieces (even though they are in the legend)

ggplot(data=fortifyResults, aes(y=lat, x=long, group=group))+
  geom_polygon(aes(fill=factor(score)),color="black")

And this one causes tearing

ggplot(data=fortifyResults, aes(y=lat, x=long), group=group)+
  geom_polygon(aes(fill=factor(score)),color="black")

comparison of example

I read that the direction of the points makes a difference. This is how the direction comes out.

ggplot(data= fortifyResults, aes(y=lat, x=long, group=group))+   
  geom_polygon(fill="yellow", color="black")+
  geom_text(aes(label=order),hjust=0, vjust=-0.3)+
  facet_wrap(~id)

enter image description here

Again, the one titled "outer" here should have a hole in the middle (hole==TRUE). From what I've read, they should go in the other direction. I've tried inverting the logic, changing the direction of the points, and a bunch of other stuff. I hope you folks can help.

Upvotes: 4

Views: 588

Answers (1)

Roman
Roman

Reputation: 4989

library(ggpolypath)

ggplot(data = fortifyResults, aes(y = lat, x = long, group = group)) +
    geom_polypath(aes(fill = factor(score)), color = "black")

1

Upvotes: 2

Related Questions