如何使用geom_tile在ggplot2中制作相对图块大小? [英] How to make relative tile sizes in ggplot2 with geom_tile?

查看:716
本文介绍了如何使用geom_tile在ggplot2中制作相对图块大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一组颜色及其补充的可视化。我想用geom_tile将它们并排放置。我的问题是一些目标颜色有多个补充,所以我需要能够在补色之间均匀分割颜色。



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

  test_pair = data.frame(pair_number = c(1,1,2,3,3,3),
color = c('#5f75e6','#e6d05f','#5f75e6','#5fb9e6','#5f75e6','#b9e65f','#e68d5f'),
group = c('目标','补偿','目标','补偿','目标','补偿','补偿'),
bin_width = c(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文档。任何人都知道如何做到这一点?



解决方案

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



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

$ $ p $ test_pair = data.frame(pair_number = c(1,1,2,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()


I'm trying to make a visualization of sets of colors and their complements. I want to use geom_tile to place them side by side. My issue is that some target colors have multiple complements, so I need to be able to split the complement tile evenly between the colors.

My test dataset is this:

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))

And my plot code:

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))

The plot does split one of the complement colors in half, but centers that tile and does not plot the second complement color. I want the plot to print the two complements side by side without having to specify break-points along x like in the geom_tile documentation. Anyone know how to do this?

解决方案

The two colours are there. It's just that the x and y coordinates and the widths for the two tiles (colours) are the same; and thus, the two tiles (colours) overlap exactly. To see that that is the case, adjust the width of the underlying colour; for instance, change the bin_width line in your test_pair data frame to: bin_width = c(1, 1, 1, 1, 1, 0.75, 0.5)), then run the ggplot command.

To fix your problem, offset the x-positions of the two tiles. One way to do this is to use a numeric scale on the x-axis, then add the appropriate breaks and labels in scale_x_continuous(). But how to do the offsetting without specifying break points? I'm not sure.

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()

这篇关于如何使用geom_tile在ggplot2中制作相对图块大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆