简体   繁体   English

geom_bar 和 geom_linerange 之间的技术差异

[英]technical difference between geom_bar and geom_linerange

Experimenting with ggplot2 I noticed a difference in the graphical output between geom_bar/geom_col and geom_linerange.使用 ggplot2 进行试验时,我注意到 geom_bar/geom_col 和 geom_linerange 之间的图形输出存在差异。 As soon as I use these functions in combination with coord_polar (to create pie or donut charts) the first two outputs are pixellated whereas geom_linerange produces smooth lines.一旦我将这些函数与 coord_polar(创建饼图或圆环图)结合使用,前两个输出就会被像素化,而 geom_linerange 会产生平滑的线条。

I am fine with that.我很好。 Still I wonder why and where in the process of creating the output this difference occures?我仍然想知道为什么以及在创建输出的过程中会出现这种差异?

d <- dplyr::tibble(GRP=c("A","B","C"),
                   VAL=c(20,30,50))

p1 <- d %>%
    ggplot2::ggplot(ggplot2::aes(x=2,y=VAL,fill=GRP)) +
    ggplot2::geom_bar(width=1.5,stat="identity") +
    ggplot2::coord_polar(theta="y") +
    ggplot2::ggtitle("geom_bar") +
    ggplot2::xlim(c(0,4)) +
    ggplot2::theme_void()

p2 <- d %>%
    ggplot2::ggplot(ggplot2::aes(x=2,y=VAL,fill=GRP)) +
    ggplot2::geom_col(width=1.5) +
    ggplot2::coord_polar(theta="y") +
    ggplot2::ggtitle("geom_col") +
    ggplot2::xlim(c(0,4))  +
    ggplot2::theme_void()

p3 <- d %>%
    dplyr::mutate(YMAX=cumsum(VAL),
                  YMIN=dplyr::lag(YMAX,1,default=0)) %>%
    ggplot2::ggplot(ggplot2::aes(x=0,ymin=YMIN,ymax=YMAX,color=GRP)) +
    ggplot2::geom_linerange(size=7) +
    ggplot2::coord_polar(theta="y")  +
    ggplot2::ggtitle("geom_Linerange") +
    ggplot2::theme_void()

gridExtra::grid.arrange(p1,p2,p3)

I do see a difference on my Windows server machine with the latest R and ggplot2.我确实在装有最新 R 和 ggplot2 的 Windows 服务器机器上看到了不同之处。 This is my initial result:这是我的初步结果:

在此处输入图片说明

You can see there is little or no antialiasing in the top two facets, but there is much better smoothing in the last facet.您可以看到前两个方面几乎没有或没有抗锯齿,但最后一个方面的平滑效果要好得多。

The difference seems to be that (on some devices at least) polygon fills aren't antialiased, but line segments are.区别似乎在于(至少在某些设备上)多边形填充没有抗锯齿,但线段是抗锯齿的。 To demonstrate this, simply add a white outline around the segments in the first two facets (by adding colour = "white" to the geom_bar call), and the circles become smooth:为了证明这一点,只需在前两个方面的线段周围添加一个白色轮廓(通过在geom_bar调用中添加colour = "white" ),圆圈就会变得平滑:

在此处输入图片说明

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

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