R 中多种图形类型的对齐(最好是在 ggplot2 或lattice 中)

发布于 2025-01-08 16:36:45 字数 530 浏览 3 评论 0原文

我想使用如下数据集创建以下类型的图表,其中包含线条、条形图和热图:

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)

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事灯 2025-01-15 16:36:46

使用lattice,设置scales=list(relation="free") - 这会给你一个关于levelplot()的警告,但对齐仍然可以正常工作。如果您希望它超级对齐,请在 levelplot() 中设置 space="top" 以使图例从热图的右侧移动到顶部。

更新:我按照OP的要求重置了一些填充并删除了标签。

library(lattice)
library(gridExtra)
#within `scales` you can manipulate different parameters of x and y axis
a = xyplot(linevar ~ pos,type="l",col=1,xlab="",ylab="",
           scales=list(relation="free",x=list(draw=F)))
#layout.heights/layout.width can tweak padding for margins
b = barchart(barvar ~ pos,col=1,horizontal=F,xlab="",ylab="",
             scales=list(relation="free",x=list(cex=0.5)),
             par.settings=list(layout.heights=list(bottom.padding = 0)))
#change region colors according to your taste and use `colorkey` to remove legend
col=gray(seq(0.3,0.8,length=6))
c = levelplot(as.matrix(heatmapvar),col.regions=col,colorkey=F,xlab="",ylab="",
              scales=list(relation="free"),
              par.settings=list(layout.heights = list(top.padding =-25)))
grid.arrange(b,a,c)

在此处输入图像描述

Using lattice, set scales=list(relation="free") - this will give you a warning for levelplot(), but the alignment still works fine. If you want it super-aligned, set space="top" in levelplot() 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.

library(lattice)
library(gridExtra)
#within `scales` you can manipulate different parameters of x and y axis
a = xyplot(linevar ~ pos,type="l",col=1,xlab="",ylab="",
           scales=list(relation="free",x=list(draw=F)))
#layout.heights/layout.width can tweak padding for margins
b = barchart(barvar ~ pos,col=1,horizontal=F,xlab="",ylab="",
             scales=list(relation="free",x=list(cex=0.5)),
             par.settings=list(layout.heights=list(bottom.padding = 0)))
#change region colors according to your taste and use `colorkey` to remove legend
col=gray(seq(0.3,0.8,length=6))
c = levelplot(as.matrix(heatmapvar),col.regions=col,colorkey=F,xlab="",ylab="",
              scales=list(relation="free"),
              par.settings=list(layout.heights = list(top.padding =-25)))
grid.arrange(b,a,c)

enter image description here

孤独岁月 2025-01-15 16:36:46

通过这种组合图,在尝试将它们连接在一起之前将它们单独制作通常会更容易:

library(ggplot2)

# line plot
p.line <- ggplot(myd,aes(x=pos)) + 
          geom_line(aes(y=linevar)) + # plot the line using linevar
          # draw the scales. NOTE: position[1] should be 0 not 1.
          scale_x_continuous(breaks=position,labels=label)
# bar plot
p.bar <- ggplot(myd,aes(x=pos)) + 
         geom_bar(aes(y=barvar),stat='identity') +  # plot the bar chart
         scale_y_reverse()                          # reverse y scale

# heat plot
p.heat <- ggplot(myd,aes(x=pos)) + 
          geom_tile(aes(y=-.5,fill=heatmapvar,height=.5)) +  # draw heatmap
          scale_fill_gradient(low="yellow",high="red")       # colour scale

您可以使用 print(p.xxx) 来查看它们的外观。

现在我们可以使用gridExtra中的grid.arrange将它们组合在一起:

library(gridExtra)
grid.arrange(p.line,p.heat,p.bar)

它看起来像这样:

在此处输入图像描述

(注意 - 我的条形图发出警告,因为我有一个负的 barvar - 我假设您实际要使用的数据不会出现此问题)。

现在,热图看起来有点难看,看起来您希望将其绘制到线图上,因此我们可以将其添加到线图上:

我不希望热图在线上绘制,以便我自己设置其位置和高度:

hght <- .25 # just looked nice - you'll have to adjust based on your data
ypos <- min(myd$linevar)-hght
p.line2 <- p.line + 
           geom_tile(aes(x=pos-.5,y=ypos, fill=heatmapvar, height=hght)) +
           scale_fill_gradient(low="yellow",high="red") +               
           opts(legend.position="none")  # <-- turn off the heatmap legend.
# print(p.line2)

注意 - 我添加了 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)

现在当我安排这些时:

grid.arrange(p.line2,p.bar)

它看起来像:

在此处输入图像描述

至于调整颜色、添加标题等 - 您可以这样做。
我建议调整各个图表以达到您喜欢的方式,然后仅在最后将它们组合起来。

ggplot2 文档很有帮助(尽管我发现一般谷歌搜索“ggplot2 heatmap”(例如)是在获取示例方面更有帮助)。

With this sort of combined plot, it is often easier to make them all separately before trying to join them together:

library(ggplot2)

# line plot
p.line <- ggplot(myd,aes(x=pos)) + 
          geom_line(aes(y=linevar)) + # plot the line using linevar
          # draw the scales. NOTE: position[1] should be 0 not 1.
          scale_x_continuous(breaks=position,labels=label)
# bar plot
p.bar <- ggplot(myd,aes(x=pos)) + 
         geom_bar(aes(y=barvar),stat='identity') +  # plot the bar chart
         scale_y_reverse()                          # reverse y scale

# heat plot
p.heat <- ggplot(myd,aes(x=pos)) + 
          geom_tile(aes(y=-.5,fill=heatmapvar,height=.5)) +  # draw heatmap
          scale_fill_gradient(low="yellow",high="red")       # colour scale

You can use print(p.xxx) to see what they look like.

Now we can combine them all together using grid.arrange in gridExtra:

library(gridExtra)
grid.arrange(p.line,p.heat,p.bar)

It looks like this:

enter image description here

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

hght <- .25 # just looked nice - you'll have to adjust based on your data
ypos <- min(myd$linevar)-hght
p.line2 <- p.line + 
           geom_tile(aes(x=pos-.5,y=ypos, fill=heatmapvar, height=hght)) +
           scale_fill_gradient(low="yellow",high="red") +               
           opts(legend.position="none")  # <-- turn off the heatmap legend.
# print(p.line2)

Note - I added in a x=pos-.5 because otherwise geom_tile centres the tiles about the x value which doesn't line up (try without the x=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:

grid.arrange(p.line2,p.bar)

It looks like:

enter image description here

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

離殇 2025-01-15 16:36:46

您认为我们可以将所有三个情节合二为一吗?

根据数学咖啡的答案,我尝试用技巧来做到这一点 - 没有完全成功,但我认为专家会帮助我规避缺陷。

pos <- 1:40 # X axis
barvar <-  c(-1 * 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)

hght <- .25 # just looked nice - you'll have to adjust based on your data
ypos <- min(myd$linevar)-hght

require(ggplot2)
p.line <- ggplot(myd,aes(x=pos)) +
          geom_line(aes(y=linevar)) + # plot the line using linevar
          # draw the scales. NOTE: position[1] should be 0 not 1.
          scale_x_continuous(breaks=position,labels=label)
# bar plot
p.bar <- ggplot(myd,aes(x=pos)) +
         geom_bar(aes(y=barvar),stat='identity') +  # plot the bar chart
         scale_y_reverse()

p.line2 <- p.line +
           geom_tile(aes(x= pos-.5,y=ypos, fill=heatmapvar, height=hght)) +
           scale_fill_gradient(low="yellow",high="red") +
           opts(legend.position="none")  # <-- turn off the heatmap legend.

#
 print(p.line2)
p.line3 <- p.line2 + geom_bar(aes(y=barvar, ymax= -1),stat='identity')

print(p.line3)

在此处输入图像描述

主要存在两到三个缺陷 -

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

pos <- 1:40 # X axis
barvar <-  c(-1 * 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)

hght <- .25 # just looked nice - you'll have to adjust based on your data
ypos <- min(myd$linevar)-hght

require(ggplot2)
p.line <- ggplot(myd,aes(x=pos)) +
          geom_line(aes(y=linevar)) + # plot the line using linevar
          # draw the scales. NOTE: position[1] should be 0 not 1.
          scale_x_continuous(breaks=position,labels=label)
# bar plot
p.bar <- ggplot(myd,aes(x=pos)) +
         geom_bar(aes(y=barvar),stat='identity') +  # plot the bar chart
         scale_y_reverse()

p.line2 <- p.line +
           geom_tile(aes(x= pos-.5,y=ypos, fill=heatmapvar, height=hght)) +
           scale_fill_gradient(low="yellow",high="red") +
           opts(legend.position="none")  # <-- turn off the heatmap legend.

#
 print(p.line2)
p.line3 <- p.line2 + geom_bar(aes(y=barvar, ymax= -1),stat='identity')

print(p.line3)

enter image description here

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文