简体   繁体   English

r:在ggplot2中绘制带孔的多边形时撕裂

[英]r: 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 . 我已经尝试了很多关于group = group放置位置和fill = scale的变化

比较实际数据

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

比较例子

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)

在此输入图像描述

Again, the one titled "outer" here should have a hole in the middle (hole==TRUE). 同样,这里标题为“外部”的那个应该在中间有一个洞(洞== 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. 我希望你们大家可以提供帮助。

library(ggpolypath)

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

1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM