gpt4 book ai didi

r - 如何使用geom_tile在ggplot2中制作相对平铺尺寸?

转载 作者:行者123 更新时间:2023-12-04 11:54:47 26 4
gpt4 key购买 nike

我正在尝试对颜色集及其补色进行可视化。我想使用 geom_tile 将它们并排放置。我的问题是某些目标颜色有多种补色,所以我需要能够在颜色之间均匀地分割补色图块。

我的测试数据集是这样的:

test_pair = data.frame(pair_number = c(1, 1, 2, 2, 3, 3, 3), 
color = c('#5f75e6', '#e6d05f', '#5f75e6', '#5fb9e6', '#5f75e6', '#b9e65f', '#e68d5f'),
group = c('target', 'comp', 'target', 'comp', 'target', 'comp', 'comp'),
bin_width = c(1, 1, 1, 1, 1, 0.5, 0.5))

还有我的情节代码:
ggplot(test_pair, aes(x = factor(group), y = factor(pair_number), width = bin_width)) +
geom_tile(aes(fill = color)) +
scale_fill_identity() +
scale_x_discrete('', expand = c(0, 0)) +
scale_y_discrete('', expand = c(0, 0)) +
theme_bw() +
theme(line = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
plot.background = element_rect(fill = '#c4a879'),
axis.ticks = element_blank(),
axis.text.y = element_text(size = 14),
axis.text.x = element_text(size = 14),
axis.title.y = element_text(color = hex))

该图确实将一种补色分成两半,但将其平铺居中并且不绘制第二个补色。我希望绘图并排打印两个补码,而不必像 geom_tile documentation 那样沿 x 指定断点.有人知道怎么做吗?

ggplot with geom_tile not behaving as desired.

最佳答案

两种颜色都在。只是 x 和 y 坐标以及两个瓷砖(颜色)的宽度相同;因此,两个瓷砖(颜色)完全重叠。要查看是否是这种情况,请调整基础颜色的宽度;例如,更改 bin_width线路在您的 test_pair数据框到:bin_width = c(1, 1, 1, 1, 1, 0.75, 0.5)) ,然后运行 ​​ggplot 命令。

要解决您的问题,请偏移两个图块的 x 位置。一种方法是在 x 轴上使用数字刻度,然后在 scale_x_continuous() 中添加适当的中断和标签。 .但是如何在不指定断点的情况下进行偏移?我不知道。

test_pair = data.frame(pair_number = c(1, 1, 2, 2, 3, 3, 3), 
color = c('#5f75e6', '#e6d05f', '#5f75e6', '#5fb9e6', '#5f75e6', '#b9e65f', '#e68d5f'),
group = c(2, 1, 2, 1, 2, .75, 1.25),
bin_width = c(1, 1, 1, 1, 1, .5, .5))

ggplot(test_pair, aes(x = group, y = factor(pair_number), width = bin_width)) +
geom_tile(aes(fill = color)) +
scale_fill_identity() +
scale_x_continuous('', breaks = c(1,2), labels = c("comp", "target"), expand = c(0, 0)) +
scale_y_discrete('', expand = c(0, 0)) +
theme_bw()

enter image description here

关于r - 如何使用geom_tile在ggplot2中制作相对平铺尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21029064/

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