带多个孔的geom_polygon [英] geom_polygon with multiple hole

查看:156
本文介绍了带多个孔的geom_polygon的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我参考这个答案问题,并有其他问题。



我修改代码如下:

pre > library(ggplot2)

id< - letters [1:2]

用于填充颜色的ID和值
值< - data.frame(
id = ids,
value = c(4,5)


#多边形位置
位置< ; - data.frame(
id = c(rep(ids,each = 10),rep(b,5)),
#shape hole shape hole
x = c(1 ,4,4,1,1,2,2,3,3,2,5,10,10,5,5,6,6,7,7,6,8,8,9,9,8),
y = c(1,1,4,4,1,2,3,3,2,2,5,5,10,10,5,6,7,7,6,6,8,9 ,9,8,8)


#合并位置和值
datapoly< - merge(values,positions,by = c(id))

图< - ggplot(datapoly,aes(x = x,y = y))+
geom_polygon(aes(group = id,fill = factor(value)),color =grey )+
scale_fill _discrete(Key)

并给出以下输出:



有一条线穿过两个彩色框,我不太喜欢它,我怎么去除它?谢谢。

解决方案

试试这个

  ggplot(datapoly,aes(x = x,y = y))+ 
geom_polygon(aes(group = id,fill = factor(value))+
scale_fill_discrete(Key )


I refer to the answer for this question and have additional question.

I have modify the code as below:

library(ggplot2)

ids <- letters[1:2]

# IDs and values to use for fill colour
values <- data.frame(
  id = ids,
  value = c(4,5)
)

# Polygon position
positions <- data.frame(
  id = c(rep(ids, each = 10),rep("b",5)),
  #     shape      hole         shape        hole
  x = c(1,4,4,1,1, 2,2,3,3,2,   5,10,10,5,5, 6,6,7,7,6, 8,8,9,9,8),
  y = c(1,1,4,4,1, 2,3,3,2,2,   5,5,10,10,5, 6,7,7,6,6, 8,9,9,8,8)
)

# Merge positions and values
datapoly <- merge(values, positions, by=c("id"))

chart <- ggplot(datapoly, aes(x=x, y=y)) + 
  geom_polygon(aes(group=id, fill=factor(value)),colour="grey") +
  scale_fill_discrete("Key")

And gives the following output:

There is a line passing through the two colored boxes, which I don't quite like it, how can I remove that? Thanks.

解决方案

Try this one

ggplot(datapoly, aes(x=x, y=y)) +
  geom_polygon(aes(group=id, fill=factor(value))) +
  scale_fill_discrete("Key")

这篇关于带多个孔的geom_polygon的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆