- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下数据框:
# Dummy data frame
df <- expand.grid(x = 1:3, y = 1:3)
我想像这样使用
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)
这使,
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
最佳答案
如其他人所述,这是由于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
}
}
geom_tile
层的创建由
rectGrob
支持,其硬编码
lineend
参数值为“butt”。下图(找到
here)很好地说明了3个
lineend
值之间的差异:
GeomTile
的功能并更改代码中所有
geom_tile
层的图形参数,则可以执行此操作。 (我最近用该解决方案回答了类似的
question。)但是,对于单个绘图,我只是将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)
# this further "sharpens" the other three corners
gp$grobs[[which(grepl("panel", gp$layout$name))]]$children[[3]]$gp$linejoin <- "mitre"
grid.draw(gp)
geom_tile
对应的正确grob的实际位置不一定是
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
的输出,以标识正确的位置编号。
关于r - 角落缺少geom_tile边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53767758/
我有一个包含 5 列的数据框: N1 mydf$N1 levels(mydf$N1) #[1] "20" "30" "40" "50" "60" "70" "80" "100" "1
自从切换到 ggplot2 2.0.0 后,在 美学< 之外定义瓷砖的 width 和 height 时出现问题。我使用 geom_tile 创建了某种热图,我想在图 block 之间留一点空间,所以
自从切换到 ggplot2 2.0.0 后,在 美学< 之外定义瓷砖的 width 和 height 时出现问题。我使用 geom_tile 创建了某种热图,我想在图 block 之间留一点空间,所以
这个问题在这里已经有了答案: Margin adjustments when using ggplot's geom_tile() (1 个回答) 8年前关闭。 您可以看到一组有水平灰色边缘,而另一组
set.seed(1990) ID ggplot(dataset + , + aes(x=datetime,y= reorder(ID, n),fill=n))+ +
library(tidyverse) ## make data set.seed(3) dat % tbl_df() %>% rownames_to_column('Var1') %>%
使用 geom_tile,我想在每行数据之间放置一条水平线。看起来如果我在美学上使用颜色,它会在水平和垂直方向上放置线条,这不是我想要的。过去,我只是让瓷砖的高度变小,所以它们之间有一些间隔。但是,似
我正在尝试制作一个按 x 轴上的两个离散变量排序的分面 ggplot。问题是我想让垂直相邻的条目都接触。目前,行与行之间存在空间,具体取决于顶部图中与底部图中的因子水平。抱歉,这个可重现的例子有点冗长
我有两个不同的数据框:P1 和 P2。这些数据帧中的每一个都有 3 个不同的列:N1、N2 和一个值 (mean_RMSE)。 N1和N2在15到120之间,对于一条线,N2总是低于N1。 如果我绘制
我试图显示对 2 个调查问题的响应的密度和相关性。每个问题的答案都是因素 1-5,我想制作一个响应组合表的热图。 我的表格是这样的: > table(mydata$v47, mydata$v78)
我有一个数据框,其中包含来自 20 多个国家/地区的 20 多种产品中每种产品的订单数据。我已使用 ggplot2 将其放在突出显示表中使用与此类似的代码: require(ggplot2) requ
我有一个数据框,我想生成一个 geom_tile()从中绘制,但我希望图形的排序不是基于字母顺序而是基于此数据框中的变量。 structure(list(V1 = c("a", "y", "w", "
我有兴趣在 ggplot2 中使用 geom_tile 构建热图,该热图根据某个因素使用不同的渐变高颜色。 下图创建了一个图,其中各个图 block 根据 xy_type 被着色为蓝色或红色,但没有渐
我想创建一个图表来显示每天的工作时间表。我必须处理的数据看起来像这样: dput(head (TiNiSWeek, 3)) structure(list(`04:00` = c(0, 0, 0)
我正在尝试生成 heatmap使用 ggplot 绘图的 geom_tile .我的数据的行数远多于列数。 set.seed(1) df <- data.frame(val=rnorm(100
我用过 geom_tile()用于在同一张图上绘制 3 个变量...... tile_ruined_coop% group_by("bonus","malus","type","coop") %>%
我试过很多次,一个简单的光栅绘图是空的。但是可以绘制子数据框。所以我考虑了 ggplot 中的一些错误?数据框不是很大,大约 10k 行。 df % slice(1:1000) %>% ggp
我堆叠了一个数据框,它显示 group 中每个 id 的 value: df # A tibble: 11 x 3 #> id value group #> #> 1
我需要帮助为 86x86 矩阵分配正确的十六进制颜色代码到 ggplot + geom_tile()。它是一个相关矩阵,我想根据它的值和由 kmeans 聚类生成的类来给它着色。有六种不同的簇/颜色。
假设我有一个像这样的 data.frame: df <- matrix( rnorm(100), nrow = 10) rownames(df) <- LETTERS[1:10] molten <-
我是一名优秀的程序员,十分优秀!