简体   繁体   中英

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. 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.

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. 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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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