R:ggplot2 geom_segment像素化

问题描述

当我使用geom_segment时,与geom_abline相比它是像素化的。当我用ggsave导出到pdf时,它没有被矢量化。

geom_abline:

enter image description here

geom_segment:

enter image description here

有什么办法解决这个问题吗?

谢谢

解决方法

是的,简短的答案:使用annotate()代替geom_segment()

长回答,无论您使用geom_segment()为何,都将数据中的每一行映射到几何的美观度(在这种情况下为坐标)。因此,它们来自geom_segment()的PDF彼此之间包含nrow(your_data)行,给人以像素化的印象。

如果检查图的图层数据,则可以看到这种情况。特别要注意第二个图的图层数据。

library(ggplot2)

defaults <- list(
  coord_cartesian(expand = FALSE),theme(axis.line = element_line(colour = "black"))
)


g <- ggplot(iris,aes(x,y)) +
  geom_abline(intercept = 0,slope = 1) +
  defaults
(layer_data(g))
#>   intercept slope PANEL group colour size linetype alpha
#> 1         0     1     1    -1  black  0.5        1    NA

g <- ggplot(iris,y)) +
  geom_segment(x = 0,xend = 1,y = 0,yend = 1) +
  defaults
(head(layer_data(g)))
#>   PANEL group colour size linetype alpha x y xend yend
#> 1     1    -1  black  0.5        1    NA 0 0    1    1
#> 2     1    -1  black  0.5        1    NA 0 0    1    1
#> 3     1    -1  black  0.5        1    NA 0 0    1    1
#> 4     1    -1  black  0.5        1    NA 0 0    1    1
#> 5     1    -1  black  0.5        1    NA 0 0    1    1
#> 6     1    -1  black  0.5        1    NA 0 0    1    1

g <- ggplot(iris,y)) +
  annotate("segment",x = 0,yend = 1) +
  defaults
(layer_data(g))
#>   x xend y yend PANEL group colour size linetype alpha
#> 1 0    1 0    1     1    -1  black  0.5        1    NA

reprex package(v0.3.0)于2020-10-01创建