如何删除geom_raster中连续x值之间的空格/间隙 [英] How can I remove space/gaps between continuous x-values in geom_raster
问题描述
我正在处理一些时频分解的EEG数据,并希望使用ggplot2生成类似于频谱图的图形.但是,我最终在每个时间点之间都留有空白.
I am working with some time-frequency decomposed EEG data and want to produce a spectrogram-like figure using ggplot2. But, I end up with blank spaces between each of my time points.
Data <- read.csv(url("https://www.dropbox.com/s/al3cygigm86mr3s/Test_Spec_Data.csv?dl=0"))
如果创建香草geom_raster,则会在x和y数据中出现间隙:
If I create a vanilla geom_raster I get gaps in the x and y data:
ggplot(Data,aes(Times,Frequency)) +
geom_raster(aes(fill = ERSP))
如果我将 Frequency
(频率)设置为一个因子,它将填补y差距;但是,沿x轴的间隙仍然存在:
If I make Frequency
a factor, it fills in the y gap; but, the gaps along the x-axis remain:
ggplot(Data,aes(Times,factor(round(Frequency,digits=1)))) +
geom_raster(aes(fill = ERSP))
我可以通过将 Times
作为一个因素来消除差距.
I can eliminate the gaps by making Times
a factor.
但是,用这么多数据点管理 scale_x_discrete
很麻烦(请注意x轴标签).同样,这些时间数据是连续的,并非真正像因子.
But, managing scale_x_discrete
with this many data points is cumbersome (note the x-axis labels). Also, these time data are continuous and not really factor-like.
geom_raster
没有像 geom_bar
这样的 width
参数,并且我在 geom_raster 中看不到任何类似的东西代码>文档.
geom_raster
doesn't have a width
argument like geom_bar
and I can't see anything similar in the geom_raster
documentation.
有没有一种方法可以使 Times
保持连续,但是可以消除观察之间的差距?
Is there a way to keep Times
as continuous but remove the gaps between observations?
推荐答案
存在差距是因为没有足够的数据(或更准确地说,它们之间的间隔不是均匀的).
There are gaps because there are not enough data (or more precisely, they are not evenly spaced).
您的因子"转换消除了间隙,因为它消除了缺少数据的X轴或Y轴部分.在Y轴上看到:刻度线是均匀的空格,但值却不是(8.5-8.1 = 0.4,而11.3-10.7 = 0.6,这是您最大的差距).
Your "factor" transformation remove the gap because it remove the X or Y axis parts where data are missing. See in Y axis: ticks are evenly spaces but values are not (8.5-8.1=0.4, while 11.3-10.7=0.6, where you have you biggest gap).
我可以看到两种解决方案:
I can see two solutions:
- 插值数据,以便源数据均匀分布
- 使用
geom_tile
而不是geom_raster
并指定width
和height
参数来扩展"图块并填充差距,如文档的第四个示例所述.
- Interpolate data so your source data are evenly spaced
- Use
geom_tile
instead ofgeom_raster
and specifywidth
andheight
parameters to "expand" your tiles and fill the gaps, as explained in the fourth example of the doc.
这篇关于如何删除geom_raster中连续x值之间的空格/间隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!