R 中多种图形类型的对齐(最好是在 ggplot2 或lattice 中)
我想使用如下数据集创建以下类型的图表,其中包含线条、条形图和热图:
pos <- 1:40 # X axis
barvar <- rnorm (40, 5, 2) # y axis for barplot
linevar <- rnorm (40, 0, 0.5) # yvar for line plot
heatmapvar <- rep(1:5, 8) # yvar plot for heatmap (color coded) plot
myd <- data.frame (pos, barvar, linevar, heatmapvar)
# labeling positions
label <- c("A", "B", "C", "D", "E", "F", "G", "H", "I")
position <- c(1, 5, 10, 15, 20, 25, 30, 35, 40)
I want to create the following type of graph with line, bar and heatmap with a dataset like this:
pos <- 1:40 # X axis
barvar <- rnorm (40, 5, 2) # y axis for barplot
linevar <- rnorm (40, 0, 0.5) # yvar for line plot
heatmapvar <- rep(1:5, 8) # yvar plot for heatmap (color coded) plot
myd <- data.frame (pos, barvar, linevar, heatmapvar)
# labeling positions
label <- c("A", "B", "C", "D", "E", "F", "G", "H", "I")
position <- c(1, 5, 10, 15, 20, 25, 30, 35, 40)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用lattice,设置
scales=list(relation="free")
- 这会给你一个关于levelplot()
的警告,但对齐仍然可以正常工作。如果您希望它超级对齐,请在levelplot()
中设置space="top"
以使图例从热图的右侧移动到顶部。更新:我按照OP的要求重置了一些填充并删除了标签。
Using lattice, set
scales=list(relation="free")
- this will give you a warning forlevelplot()
, but the alignment still works fine. If you want it super-aligned, setspace="top"
inlevelplot()
to get the legend moved from the right side to the top of heatmap.Update: I reset some padding and removed labels, as OP requested.
通过这种组合图,在尝试将它们连接在一起之前将它们单独制作通常会更容易:
您可以使用
print(p.xxx)
来查看它们的外观。现在我们可以使用
gridExtra
中的grid.arrange
将它们组合在一起:它看起来像这样:
(注意 - 我的条形图发出警告,因为我有一个负的
barvar
- 我假设您实际要使用的数据不会出现此问题)。现在,热图看起来有点难看,看起来您希望将其绘制到线图上,因此我们可以将其添加到线图上:
我不希望热图在线上绘制,以便我自己设置其位置和高度:
注意 - 我添加了
x=pos-.5
因为否则geom_tile 将图块围绕未对齐的 x 值居中(尝试不使用
x=pos-.5
看看我的意思)。标签位置
label <- c("A", "B", "C", "D", "E", "F", "G", "H", "I")
位置 <- c(1, 5, 10, 15, 20, 25, 30, 35, 40)
现在当我安排这些时:
它看起来像:
至于调整颜色、添加标题等 - 您可以这样做。
我建议调整各个图表以达到您喜欢的方式,然后仅在最后将它们组合起来。
ggplot2 文档很有帮助(尽管我发现一般谷歌搜索“ggplot2 heatmap”(例如)是在获取示例方面更有帮助)。
With this sort of combined plot, it is often easier to make them all separately before trying to join them together:
You can use
print(p.xxx)
to see what they look like.Now we can combine them all together using
grid.arrange
ingridExtra
:It looks like this:
(Note - my bar chart gave a warning because I have a negative
barvar
- I assume the data you actually are going to use won't have this problem).Now, the heat map looks a bit ugly and it looks like you want it drawn onto the line map, so we can add it on to the line map instead:
I don't want the heat map to be drawn over the line so I'll set its pos and height myself:
Note - I added in a
x=pos-.5
because otherwisegeom_tile
centres the tiles about the x value which doesn't line up (try without thex=pos-.5
to see what I mean).labeling positions
label <- c("A", "B", "C", "D", "E", "F", "G", "H", "I")
position <- c(1, 5, 10, 15, 20, 25, 30, 35, 40)
Now when I arrange these:
It looks like:
As to tweaking colours, adding in titles, etc - you can do that.
I'd recommending tweaking the individual graphs to get them how you like, and then only combine them at the end.
The ggplot2 documentation is helpful (although I find that general googling for 'ggplot2 heatmap' (e.g.) is more helpful in terms of getting examples to work off).
您认为我们可以将所有三个情节合二为一吗?
根据数学咖啡的答案,我尝试用技巧来做到这一点 - 没有完全成功,但我认为专家会帮助我规避缺陷。
主要存在两到三个缺陷 -
(1) 无法使条形的起始点从 - 2
(2) 我想抑制 - 负数(我只是使用技巧将条形转换为负数)
Can you think that we can combine all three plots into one ?
Based on mathematical coffee's answer, I tried to do so with tricks - not completely successful however I think experts will help me to circumvent the deficiencies.
There are major two - three deficiencies -
(1) In could not make the start point of bars from - 2
(2) I want to suppress - negative number (I just use the trick to convert the bars to negative)