角落处缺少geom_tile边界

Lyn*_*akr 9 r ggplot2

我有以下数据框:

# Dummy data frame
df <- expand.grid(x = 1:3, y = 1:3)
Run Code Online (Sandbox Code Playgroud)

我想把它描绘成一个像这样的geom_tile使用ggplot2:

# Tile plot
ggplot(df) + 
  geom_tile(aes(x = x, y = y), 
            fill = NA, colour = "red", size = 3, width = 0.7, height = 0.7)
Run Code Online (Sandbox Code Playgroud)

这使,

在此输入图像描述

但请注意,在每个瓷砖的左上角,缺少一个缺口,其中边框不能正确地吻合.如果我使用,我会得到相同的结果geom_rect.有没有解决方法来避免这种情况?


R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default  

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_3.1.0     kitchendraw_0.1.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0       rstudioapi_0.8   bindr_0.1.1      magrittr_1.5     tidyselect_0.2.5 munsell_0.5.0    colorspace_1.3-2
 [8] R6_2.3.0         rlang_0.3.0.1    plyr_1.8.4       dplyr_0.7.7      tools_3.5.1      grid_3.5.1       gtable_0.2.0    
[15] withr_2.1.2      yaml_2.2.0       lazyeval_0.2.1   assertthat_0.2.0 tibble_1.4.2     crayon_1.3.4     bindrcpp_0.2.2  
[22] purrr_0.2.5      glue_1.3.0       labeling_0.3     compiler_3.5.1   pillar_1.3.0     scales_1.0.0     pkgconfig_2.0.2 
Run Code Online (Sandbox Code Playgroud)

更新后的数字以回应下面的评论

在此输入图像描述

Z.L*_*Lin 6

正如其他人指出的,这是由于lineend规范所致,可以在以下位置找到environment(GeomTile$draw_panel)$f

function (self, data, panel_params, coord) 
{
    if (!coord$is_linear()) {
        ... #omitted for space
    }
    else {
        coords <- coord$transform(data, panel_params)
        ggname("geom_rect", 
               rectGrob(coords$xmin, coords$ymax, 
                        width = coords$xmax - coords$xmin, height = coords$ymax - 
                        coords$ymin, default.units = "native", just = c("left", "top"), 
                        gp = gpar(col = coords$colour, 
                                  fill = alpha(coords$fill, coords$alpha), 
                                  lwd = coords$size * .pt, 
                                  lty = coords$linetype, 
                                  lineend = "butt"))) # look here
    }
}
Run Code Online (Sandbox Code Playgroud)

geom_tile层的创建由rectGrob硬支持,lineend参数值为“对接”。下图(在此处找到)lineend很好地说明了这三个值之间的差异:

线端值

如果您想深入研究底层GeomTile的功能并更改geom_tile代码中所有图层的图形参数,则可以这样做。(我最近通过该解决方案回答了类似的问题。)但是,对于单个绘图,我只是将ggplot转换为grob对象,并搞乱了gp那里的参数:

library(grid)
gp <- ggplotGrob(p)
grid.draw(gp) 

# this "sharpens" the top left corner
gp$grobs[[which(grepl("panel", gp$layout$name))]]$children[[3]]$gp$lineend <- "square"
grid.draw(gp)
Run Code Online (Sandbox Code Playgroud)

更改线端后绘制

# this further "sharpens" the other three corners
gp$grobs[[which(grepl("panel", gp$layout$name))]]$children[[3]]$gp$linejoin <- "mitre"
grid.draw(gp)
Run Code Online (Sandbox Code Playgroud)

更改线连接后绘制

注意:对应的正确grob的实际位置geom_tile不一定是gp$grobs[[which(grepl("panel", gp$layout$name))]]$children[[3]]$gp$linejoin。它在children[[3]]这里,但是在ggplot对象中的其他geom层(位于该层之下或之上)geom_tile可以移动其相对位置。在这种情况下,您可能需要检查gp$grobs[[which(grepl("panel", gp$layout$name))]]$children控制台中的输出以识别正确的位置编号。

  • @Lyngbakr在这种情况下,lineend的名称为square,linejoin的名称为mitre。我认为目前根本没有指定“ linejoin”,因此其值取决于所选图形设备中根视口的设置。ggplot2小组可能有理由在行尾选择“对接”而不是“正方形”(例如,线元素的更精确的终点),但是可能值得考虑将“ lineend” /“ linejoin”作为`geom_XXX( )`层,即使`lineend =“ butt”`仍然是默认值... (3认同)

Lyn*_*akr 1

该问题现已通过此更新得到解决ggplot2