Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

ggplot2 - R: How to combine straight lines of polygon and line segments with polar coordinates?

Let's say I have linear data in 6 directions with some lengths. I want to make chart in style of "wind rose".

###create sample data
a <- c(1,2,3,4,5,6) #directions
perc <- c(0.15,0.05,0.3,0.15,0.05,0.3) #percentual lengths
lab <- c("A", "B", "C", "D", "E", "F") #labels of directions
data <- data.frame(a,perc,lab)

I have tried two variants with ggplot2, using coord_polar and coord_radar (inspired by an article from Erwan Le Pennec: From Parallel Plot to Radar Plot). Each one is partly correct and partly wrong (from view of my expectation):

#similar parameters in both variants:
chart_stuff <- list(
  geom_polygon(aes(x=a, y=perc, col = 1), fill=NA,show.legend = F),
  geom_segment(aes(x=as.factor(a), yend=perc, xend=as.factor(a), y=0), size=2),
  scale_x_discrete(labels=data$lab), 
  scale_y_continuous(labels = scales::percent, limits = c(0,0.31)), 
  theme_light(), 
  theme(axis.title = element_blank())
  )

#chart1
ggplot(data) +  
  chart_stuff+
  coord_polar(start=(-pi/6))+
  ggtitle("coord_polar: wrong polygon, good segments")

#chart2
#coord_radar function with modified start parameter:
coord_radar <- function (theta = "x", start = -pi/6, direction = 1) {
  theta <- match.arg(theta, c("x", "y"))
  r <- if (theta == "x") "y" else "x"
  ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start, 
          direction = sign(direction),
          is_linear = function(coord) TRUE)
}

ggplot(data) +   
  chart_stuff+
  coord_radar()+
  ggtitle("coord_radar: good polygon, wrong segments")

Outputs:

from https://i.ibb.co/0c8LJrt/charts.png

So I want one image with straight lines of polygon border and segments representing directions (in length of percentage). I guess error might be in mixing discrete scale with continuous but I can't solve it. Any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Explanation

In GeomSegment's draw_panel function, whether the coordinate system is linear or not affects how the panel is drawn:

> GeomSegment$draw_panel
<ggproto method>
  <Wrapper function>
    function (...) 
f(...)

  <Inner function (f)>
    function (data, panel_params, coord, arrow = NULL, arrow.fill = NULL, 
    lineend = "butt", linejoin = "round", na.rm = FALSE) 
{
    data <- remove_missing(data, na.rm = na.rm, c("x", "y", "xend", 
        "yend", "linetype", "size", "shape"), name = "geom_segment")
    if (empty(data)) 
        return(zeroGrob())
    if (coord$is_linear()) {
        coord <- coord$transform(data, panel_params)
        arrow.fill <- arrow.fill %||% coord$colour
        return(segmentsGrob(coord$x, coord$y, coord$xend, coord$yend, 
            default.units = "native", gp = gpar(col = alpha(coord$colour, 
                coord$alpha), fill = alpha(arrow.fill, coord$alpha), 
                lwd = coord$size * .pt, lty = coord$linetype, 
                lineend = lineend, linejoin = linejoin), arrow = arrow))
    }
    data$group <- 1:nrow(data)
    starts <- subset(data, select = c(-xend, -yend))
    ends <- plyr::rename(subset(data, select = c(-x, -y)), c(xend = "x", 
        yend = "y"), warn_missing = FALSE)
    pieces <- rbind(starts, ends)
    pieces <- pieces[order(pieces$group), ]
    GeomPath$draw_panel(pieces, panel_params, coord, arrow = arrow, 
        lineend = lineend)
}

coord_polar is not linear, because by default CoordPolar$is_linear() evaluates to FALSE, so geom_segment is drawn based on GeomPath$draw_panel(...).

coord_radar, on the other hand, is linear, because is_linear = function(coord) TRUE is included in its definition, so geom_segment is drawn using segmentsGrob(...).

Workaround

We can define our own version of GeomSegment, which uses the former option for draw_panel regardless whether the coordinate system is linear:

GeomSegment2 <- ggproto("GeomSegment2",
                        GeomSegment,
                        draw_panel = function (data, panel_params, coord, arrow = NULL,
                                               arrow.fill = NULL, lineend = "butt", 
                                               linejoin = "round", na.rm = FALSE) {
                          data <- remove_missing(data, na.rm = na.rm, 
                                                 c("x", "y", "xend", "yend", "linetype", 
                                                   "size", "shape"), 
                                                 name = "geom_segment")                          
                          if (ggplot2:::empty(data)) 
                            return(zeroGrob())
                          # remove option for linear coordinate system
                          data$group <- 1:nrow(data)
                          starts <- subset(data, select = c(-xend, -yend))
                          ends <- plyr::rename(subset(data, select = c(-x, -y)), 
                                               c(xend = "x", yend = "y"), 
                                               warn_missing = FALSE)
                          pieces <- rbind(starts, ends)
                          pieces <- pieces[order(pieces$group), ]
                          GeomPath$draw_panel(pieces, panel_params, coord, arrow = arrow, 
                                              lineend = lineend)
                        })

geom_segment2 <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity", 
                           ..., arrow = NULL, arrow.fill = NULL, lineend = "butt", 
                           linejoin = "round", na.rm = FALSE, show.legend = NA, 
                           inherit.aes = TRUE) {
  layer(data = data, mapping = mapping, stat = stat, geom = GeomSegment2, 
        position = position, show.legend = show.legend, inherit.aes = inherit.aes, 
        params = list(arrow = arrow, arrow.fill = arrow.fill, 
                      lineend = lineend, linejoin = linejoin, na.rm = na.rm, 
                      ...))
}

Try it out:

chart_stuff <- list(
  geom_polygon(aes(x=a, y=perc, col = 1), fill=NA,show.legend = F),
  # geom_segment2 instead of geom_segment
  geom_segment2(aes(x=as.factor(a), yend=perc, xend=as.factor(a), y=0), size=2), 
  scale_x_discrete(labels=data$lab), 
  scale_y_continuous(labels = scales::percent, limits = c(0,0.31)), 
  theme_light(), 
  theme(axis.title = element_blank())
)

ggplot(data) +   
  chart_stuff+
  coord_radar()+
  ggtitle("coord_radar: good polygon, good segments")

plot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...