gpt4 book ai didi

r - 在热图的图例连续渐变颜色条周围绘制边框

转载 作者:行者123 更新时间:2023-12-03 23:20:35 28 4
gpt4 key购买 nike

如何在连续渐变色条周围添加边框。默认情况下,ggplot拾取scale_fill_gradient中指定的填充颜色。

我找到的最接近的答案是这个one,但是它没有帮助我完成此任务。

我也用图例键尝试过此操作,但并没有帮助我。

legend.key = element_rect(colour = "black", size = 4)


请查看下面的当前和预期图表。

数据:

df1 <- structure(list(go = structure(c(17L, 16L, 15L, 14L, 13L, 12L, 
11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 17L, 16L, 15L,
14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L,
17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L,
3L, 2L, 1L, 17L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L,
6L, 5L, 4L, 3L, 2L, 1L),
.Label = c("q", "p", "o", "n", "m", "l", "k", "j", "i", "h", "g", "f", "e", "d", "c", "b", "a"),
class = c("ordered", "factor")),
variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L),
class = "factor", .Label = c("a", "b", "c", "d")),
value = c(-0.626453810742332, 0.183643324222082, -0.835628612410047, 1.59528080213779, 0.329507771815361,
-0.820468384118015, 0.487429052428485, 0.738324705129217, 0.575781351653492,
-0.305388387156356, 1.51178116845085, 0.389843236411431, -0.621240580541804,
-2.2146998871775, 1.12493091814311, -0.0449336090152309, -0.0161902630989461,
0.943836210685299, 0.821221195098089, 0.593901321217509, 0.918977371608218,
0.782136300731067, 0.0745649833651906, -1.98935169586337, 0.61982574789471,
-0.0561287395290008, -0.155795506705329, -1.47075238389927, -0.47815005510862,
0.417941560199702, 1.35867955152904, -0.102787727342996, 0.387671611559369,
-0.0538050405829051, -1.37705955682861, -0.41499456329968, -0.394289953710349,
-0.0593133967111857, 1.10002537198388, 0.763175748457544, -0.164523596253587,
-0.253361680136508, 0.696963375404737, 0.556663198673657, -0.68875569454952,
-0.70749515696212, 0.36458196213683, 0.768532924515416, -0.112346212150228,
0.881107726454215, 0.398105880367068, -0.612026393250771, 0.341119691424425,
-1.12936309608079, 1.43302370170104, 1.98039989850586, -0.367221476466509,
-1.04413462631653, 0.569719627442413, -0.135054603880824, 2.40161776050478,
-0.0392400027331692, 0.689739362450777, 0.0280021587806661, -0.743273208882405,
0.188792299514343, -1.80495862889104, 1.46555486156289)),
.Names = c("go", "variable", "value"), row.names = c(NA, -68L), class = "data.frame")


码:

library('ggplot2')
ggplot( data = df1, mapping = aes( x = variable, y = go ) ) + # draw heatmap
geom_tile( aes( fill = value ), colour = "white") +
scale_fill_gradient( low = "white", high = "black",
guide = guide_colorbar(label = TRUE,
draw.ulim = TRUE,
draw.llim = TRUE,
ticks = TRUE,
nbin = 10,
label.position = "bottom",
barwidth = 13,
barheight = 1.3,
direction = 'horizontal')) +
scale_y_discrete(position = "right") +
scale_x_discrete(position = "top") +
coord_flip() +
theme(axis.text.x = element_text(),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
legend.position = 'bottom',
legend.title = element_blank(),
legend.key = element_rect(colour = "black", size = 4)
)


当前图:

enter image description here

预期图表:

enter image description here

最佳答案

截至2018年5月2日,这已经可以在ggplot2的github版本中使用,并将成为ggplot2 2.3的一部分:

# devtools::install_github("tidyverse/ggplot2") # do this once, to install github version
library(ggplot2)
ggplot( data = df1, mapping = aes( x = variable, y = go ) ) + # draw heatmap
geom_tile( aes( fill = value ), colour = "white") +
scale_fill_gradient( low = "white", high = "black",
guide = guide_colorbar(label = TRUE,
draw.ulim = TRUE,
draw.llim = TRUE,
# here comes the code change:
frame.colour = "black",
ticks = TRUE,
nbin = 10,
label.position = "bottom",
barwidth = 13,
barheight = 1.3,
direction = 'horizontal')) +
scale_y_discrete(position = "right") +
scale_x_discrete(position = "top") +
coord_flip() +
theme(axis.text.x = element_text(),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
legend.position = 'bottom',
legend.title = element_blank()
)


enter image description here

具体来说,以下参数已添加到 guide_colorbar()


frame.colour颜色栏周围框架的颜色。如果 NULL,则不绘制任何框架。
frame.linewidth框架的线宽。
frame.linetype框架的线型。
ticks.colour绘制的刻度线的颜色。
ticks.linewidth刻度线的宽度。


这五个参数应允许最常用的样式。我认为,更复杂的样式(例如外面的刻度线)将需要对指南的主题进行重新思考。

关于r - 在热图的图例连续渐变颜色条周围绘制边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50070741/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com