【问题标题】:Fontsize error when creating new Geom in ggplot2在 ggplot2 中创建新 Geom 时出现字体大小错误
【发布时间】:2017-01-31 19:58:51
【问题描述】:

我正在尝试创建一个新的几何图形,它将根据飓风数据创建一个风半径图。

运行此程序所需的数据来自以下内容:

storm_observation <- data_frame(longitude = c(-89.6, -89.6, -89.6),
                            latitude = c(29.5, 29.5, 29.5),
                            wind_speed = c("34", "50", "64"),
                            ne = c(200, 120, 90),
                            nw = c(100, 75, 60),
                            se = c(200, 120, 90),
                            sw = c(150, 75, 60))

我创建新 Geom 的代码包含在下面,但抛出了一个与字体大小相关的奇怪错误:

check.length(gparname) 中的错误:“gpar”元素“fontsize”的长度不得为 0

我尝试在default_aesgpar() 函数中包含字体大小,但仍然导致同样的错误。任何帮助,将不胜感激。注意:这需要 tidyrdplyrgeosphere 包。

GeomHurricane <- ggproto("GeomPolygon", Geom,
                     required_aes = c("x", "y", "r_ne", "r_se", "r_nw", "r_sw",
                                      "fill", "colour"),
                     default_aes = aes(scale_radii = 0.8, alpha = 0.8, linetype = 1, size = 0.5),


                     draw_group = function(data, panel_scales, coord) {

                       ## Create function for conditional mutation
                       mutate_cond <- function(.data, condition, ..., envir = parent.frame()) {
                         condition <- eval(substitute(condition), .data, envir)
                         .data[condition, ] <- .data[condition, ] %>% mutate(...)
                         .data
                       }

                       ## Create df of bearings for later joining
                       bearingDF <- tibble::data_frame(bearing = c(360,1:90,90:180,180:270,270:360),
                                                       direction = rep(c("r_ne", "r_se", "r_sw", "r_nw"),
                                                                       each = 91)) %>%
                         dplyr::bind_rows(tibble::data_frame(bearing = rep(0, 4),
                                                             direction = c("r_ne", "r_se", "r_sw", "r_nw")))

                       ## Transform data in tidy format and combine with bearings
                       data <- data %>%
                         dplyr::select(x, y, r_ne, r_nw, r_se, r_sw, colour, fill,
                                       PANEL, group, scale_radii, alpha, linetype,
                                       size) %>%
                         tidyr::gather(direction, distance, -x, -y, -colour, -fill,
                                       -PANEL, -group, -scale_radii, -alpha, -linetype,
                                       -size) %>%
                         dplyr::mutate(distance = distance * 1852 * scale_radii) %>%
                         dplyr::left_join(bearingDF, by = "direction") %>%
                         mutate_cond(bearing == 0, distance = 0)

                       ## Generate correct lat/lon for perimeter of polygons
                       data <- data %>%
                         dplyr::bind_cols(as.data.frame(geosphere::destPoint(as.matrix(data[,1:2]),
                                                                             data$bearing,
                                                                             data$distance))) %>%
                         dplyr::select(-x, -y) %>%
                         dplyr::rename(x = lon, y = lat)

                       ## Coord transform and take first row
                       coords <- coord$transform(data, panel_scales)
                       first_row <- coords[1, , drop = FALSE]

                       grid::polygonGrob(
                         coords$x, coords$y, 
                         default.units = "native",
                         gp = grid::gpar(
                           col = first_row$colour,
                           fill = scales::alpha(first_row$fill, first_row$alpha),
                           lwd = first_row$size * .pt,
                           lty = first_row$linetype
                         )
                       )
                     })

geom_hurricane <- function(mapping = NULL, data = NULL, stat = "identity", position = "identity",
                       na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ...) {
layer(geom = GeomHurricane, mapping = mapping, data = data, stat = stat,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(na.rm = na.rm, ...))}

下面是使用新geom创建地图的代码:

get_map("Louisiana", zoom = 6, maptype = "toner-background", source = "stamen") %>%
  ggmap(extent = "device") +
  geom_hurricane(data = storm_observation,
             aes(x = longitude, y = latitude,
                 r_ne = ne, r_se = se, r_nw = nw, r_sw = sw,
                 fill = wind_speed, color = wind_speed)) +
  scale_color_manual(name = "Wind speed (kts)",
                 values = c("red", "orange", "yellow")) +
  scale_fill_manual(name = "Wind speed (kts)",
                values = c("red", "orange", "yellow"))

【问题讨论】:

  • 我的朋友知道你的疯狂数据集......你需要创建一个可重现的小例子,以便人们可以使用它......
  • 最初包含了所需的全部数据,但我已将其更改为可以创建数据帧的短代码块。
  • @Nobie,数据集可以在这里找到rammb.cira.colostate.edu/research/tropical_cyclones/…目标是提取数据并整理数据集(将宽转换为长)。将数据转换为 long 后,下一步就是创建一个 geom。我相信这就是 G.Ambrose 想要实现的目标。

标签: r ggplot2 r-grid ggproto


【解决方案1】:

我想通了。不知何故忘记包含draw_key = draw_key_polygon,一旦我将它添加回ggproto 函数,一切正常。 谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多