R语言 面的总体标签

xt0899hw  于 2023-06-19  发布在  其他
关注(0)|答案(2)|浏览(156)

下面是一些最小的代码来生成具有两组面的图。

library("ggplot2", quietly = TRUE, warn.conflicts = FALSE)
library("RColorBrewer", quietly = TRUE, warn.conflicts = FALSE)
val.a <- rnorm(10)
val.b <- rnorm(10)
val.c <- c("A","B","A","A","B","B","B","B","A","B")
val.d <- c("D","D","E","D","E","E","E","D","D","E")
val.e <- rnorm(10)
maya <- data.frame(val.a,val.b,val.c,val.d,val.e)
ggplot(maya, aes(x=val.a, y=val.b)) +
  geom_point(shape=20,size=3, aes(colour=val.e)) +
  facet_grid(val.c~val.d) +
  xlab("Leonardo") + ylab("Michaelangelo") +
  scale_colour_gradientn(colours=brewer.pal(9,"YlGnBu"), name="Splinter")

我不知道如何添加一个整体的面标签,使名称Donatello和Raphael是在顶部和右手边轴。
我在SO上看到了一些类似的解决方案,但我无法理解代码的头部或尾部。你能给我一个答案吗?
类似的问题在这里,但它失败了,如果我有两个以上的方面。标签显示在图中的某个地方。有没有一种方法可以使这种方法适用于一般情况?
所以我在上面的链接中尝试了rawr的解决方案,结果它在多个列的同一个地方结束了。下面是更新到rawr的解决方案的代码,但是它产生了一些意想不到的地方(对我来说,因为我不理解解决方案)。

library("ggplot2", quietly = TRUE, warn.conflicts = FALSE)
library("RColorBrewer", quietly = TRUE, warn.conflicts = FALSE)
val.a <- rnorm(20)
val.b <- rnorm(20)
val.c <- c("A","B","C","D","E","F","G","H","I","J")
val.d <- c("A","B","C","D","E","F","G","H","I","J")
val.e <- rnorm(20)
maya <- data.frame(val.a,val.b,val.c,val.d,val.e)
p <- ggplot(maya, aes(x=val.a, y=val.b)) + geom_point(shape=20,size=3, aes(colour=val.e)) + facet_grid(val.c~val.d) + xlab("Leonardo") + ylab("Michaelangelo") + scale_colour_gradientn(colours=brewer.pal(9,"YlGnBu"), name="Splinter")
z <- ggplotGrob(p)

library(grid)
library(gtable)
# add label for right strip
z <- gtable_add_cols(z, unit(z$width[[7]], 'cm'), 7)
z <- gtable_add_grob(z, 
                     list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))),
                          textGrob("Variable 1", rot = -90, gp = gpar(col = gray(1)))),
                     4, 8, 6, name = paste(runif(2)))

# add label for top strip
z <- gtable_add_rows(z, unit(z$heights[[3]], 'cm'), 2)
z <- gtable_add_grob(z, 
                     list(rectGrob(gp = gpar(col = NA, fill = gray(0.5))),
                          textGrob("Variable 2", gp = gpar(col = gray(1)))),
                     3, 4, 3, 6, name = paste(runif(2)))

# add margins
z <- gtable_add_cols(z, unit(1/8, "line"), 7)
z <- gtable_add_rows(z, unit(1/8, "line"), 3)

# draw it
grid.newpage()
grid.draw(z)

请有人好心地指出我的代码的一部分,告诉它的一般方面标签应该有多宽?

slwdgvem

slwdgvem1#

这是相当普遍的。顶部和右侧条带的当前位置在布局数据框中给出。此解决方案使用这些位置来定位新条带。新条带的构造使得高度、宽度、背景颜色以及字体大小和颜色与当前条带中的相同。下面有一些解释。

# Packages
library(ggplot2)
library(RColorBrewer)
library(grid)
library(gtable)

# Data
val.a <- rnorm(20)
val.b <- rnorm(20)
val.c <- c("A","B","C","D","E","F","G","H","I","J")
val.d <- c("A","B","C","D","E","F","G","H","I","J")
val.e <- rnorm(20)
maya <- data.frame(val.a,val.b,val.c,val.d,val.e)

# Base plot
p <- ggplot(maya, aes(x = val.a, y = val.b)) + 
   geom_point(shape = 20,size = 3, aes(colour = val.e)) + 
   facet_grid(val.c ~ val.d) + 
   xlab("Leonardo") + ylab("Michaelangelo") + 
   scale_colour_gradientn(colours = brewer.pal(9,"YlGnBu"), name = "Splinter")

# Labels 
labelR = "Variable 1"
labelT = "Varibale 2"

# Get the ggplot grob
z <- ggplotGrob(p)

# Get the positions of the strips in the gtable: t = top, l = left, ...
posR <- subset(z$layout, grepl("strip-r", name), select = t:r)
posT <- subset(z$layout, grepl("strip-t", name), select = t:r)

# Add a new column to the right of current right strips, 
# and a new row on top of current top strips
width <- z$widths[max(posR$r)]    # width of current right strips
height <- z$heights[min(posT$t)]  # height of current top strips

z <- gtable_add_cols(z, width, max(posR$r))  
z <- gtable_add_rows(z, height, min(posT$t)-1)

# Construct the new strip grobs
stripR <- gTree(name = "Strip_right", children = gList(
   rectGrob(gp = gpar(col = NA, fill = "grey85")),
   textGrob(labelR, rot = -90, gp = gpar(fontsize = 8.8, col = "grey10"))))

stripT <- gTree(name = "Strip_top", children = gList(
   rectGrob(gp = gpar(col = NA, fill = "grey85")),
   textGrob(labelT, gp = gpar(fontsize = 8.8, col = "grey10"))))

# Position the grobs in the gtable
z <- gtable_add_grob(z, stripR, t = min(posR$t)+1, l = max(posR$r) + 1, b = max(posR$b)+1, name = "strip-right")
z <- gtable_add_grob(z, stripT, t = min(posT$t), l = min(posT$l), r = max(posT$r), name = "strip-top")

# Add small gaps between strips
z <- gtable_add_cols(z, unit(1/5, "line"), max(posR$r))
z <- gtable_add_rows(z, unit(1/5, "line"), min(posT$t))

# Draw it
grid.newpage()
grid.draw(z)

r6vfmomb

r6vfmomb2#

仅供参考,对于那些关注这个但希望更简化实现的人来说,您可以使用ggh4x包中的facet_nested函数实现与桑迪Muspratt相同的结果。

# Packages
library(ggplot2)
library(ggh4x)
library(RColorBrewer)

# Data
val.a <- rnorm(20)
val.b <- rnorm(20)
val.c <- c("A","B","C","D","E","F","G","H","I","J")
val.d <- c("A","B","C","D","E","F","G","H","I","J")
val.e <- rnorm(20)
maya <- data.frame(val.a,val.b,val.c,val.d,val.e)

# plot
ggplot(maya, aes(x = val.a, y = val.b)) + 
  geom_point(shape = 20,size = 3, aes(colour = val.e)) + 
  facet_nested("Variable 2" * val.c ~ "Variable 1" * val.d) + 
  xlab("Leonardo") + ylab("Michaelangelo") + 
  scale_colour_gradientn(colours = brewer.pal(9,"YlGnBu"), name = "Splinter")

相关问题