删除ggplot2 geom_polygon缓冲

时间:2015-07-24 20:29:28

标签: r ggplot2

我目前正在尝试根据ggplot2网站geom_polygon上提供的示例绘制三个多边形。然而,尽管设置了xlim和ylim,它似乎强制在图像周围进行一些缓冲,有没有办法去除这个缓冲以使多边形到达边缘?

library(ggplot2)

ids <- factor(1:3)
values <- data.frame(
  id = ids,
  value = 1:3)

positions <- data.frame(
  id = rep(ids, each=4),
  x = c( -3, 0, 0, -3,
         -3, 0, 3, -3,
          0, 3, 3, 0 ),
  y = c( -3,-3, 0, 0,
          0, 0, 3, 3,
         -3,-3, 3, 0 )
  )

datapoly <- merge( values, positions, by=c('id'))

mnp.latent.plot <- ggplot(datapoly, aes(x=x,y=y, alpha=0.5)) +
geom_polygon(aes(fill=value, group=id)) + xlim(-3,3) + ylim(-3,3) +   
guides(fill=FALSE,alpha=FALSE)

plot(mnp.latent.plot)

enter image description here

1 个答案:

答案 0 :(得分:1)

将xlim和ylim放在coord_cartesian

mnp.latent.plot <- ggplot(datapoly, aes(x = x, y = y, alpha = 0.5)) + 
  geom_polygon(aes(fill = value, group = id)) + 
  guides(fill = FALSE, alpha = FALSE) + coord_cartesian(xlim = c(-3, 3), ylim = c(-3, 3))

plot(mnp.latent.plot) 

enter image description here