我有一个数据框列表,用于创建ggplots 列表,然后使用组装成一个绘图网格cowplot。然后,我需要附加一个共享的标题,字幕和标题。我想以一种方式来实现这些标签,使其具有与主题元素(大小,字体等)相同的主题,就像它们是由labs而不是由所创建的cowplot::draw_label。
在我的实际情况下,我有几个choropleth,每个choropleth都有自己的单位和比例,这就是为什么我不能只是一个方面,而是需要彼此独立地构建图。
这是数据的简化版本,以及我已加载的软件包:
library(tidyverse)
library(cowplot)
dfs <- list(
adults_no_diploma = structure(list(
tract = c("09003405100", "09003405200", "09003405300", "09003405401", "09003405402", "09003405500", "09003405600", "09003405700", "09003405800", "09003405900"),
value = c(0.08, 0.108, 0.095, 0.099, 0.105, 0.103, 0.161, 0.279, 0.056, 0.055)),
row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")),
severe_cost_burden = structure(list(
tract = c("09003405100", "09003405200", "09003405300", "09003405401", "09003405402", "09003405500", "09003405600", "09003405700", "09003405800", "09003405900"),
value = c(0.128, 0.147, 0.165, 0.1, 0.151, 0.11, 0.179, 0.184, 0.14, 0.038)), …Run Code Online (Sandbox Code Playgroud) 以下代码用于工作但不再适用.有人知道发生了什么事吗?必须对基础gtable代码进行一些更改.
require(cowplot) # for plot_grid()
require(grid) # for unit_max()
# make two plots
plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() + facet_grid(. ~ Species) + stat_smooth(method = "lm") +
background_grid(major = 'y', minor = "none") +
panel_border()
plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) +
geom_point(size=2.5)
g.iris <- ggplotGrob(plot.iris) # convert to gtable
g.mpg <- ggplotGrob(plot.mpg) # convert to gtable
iris.widths <- g.iris$widths[1:3] # extract the first three widths,
# corresponding to left margin, y …Run Code Online (Sandbox Code Playgroud) 我试图用每个方面绘制有自己的传奇的方面.但是,我遇到了一些麻烦,无法正确对齐.
dat <- structure(list(group1 = structure(c(1L, 1L, 2L, 2L, 2L), .Label = c("A",
"B"), class = "factor"), group2 = structure(c(1L, 2L, 1L, 3L,
2L), .Label = c("a", "b", "c"), class = "factor"), x = c("1",
"2", "3", "4", "2"), y = c("1", "2", "3", "4", "3")), .Names = c("group1",
"group2", "x", "y"), row.names = c(NA, 5L), class = "data.frame")
dat <- split(dat, f = dat$group1)
library(ggplot2)
p1 <- ggplot(dat$A) +
geom_point(aes(x=x, y=y, colour=group2)) +
facet_wrap(~group1) +
guides(colour=guide_legend(nrow=2)) +
scale_colour_manual(values=c(a = …Run Code Online (Sandbox Code Playgroud) 我从1:10 ggplots所谓的名单plot_1,plot_2...... plot_10。
我想使用Cowplot一起显示所有图。
如何plot.grid()调用所有地块?即我想写点东西
plot.grid(paste0("plot",1:10))
Run Code Online (Sandbox Code Playgroud)
但这不起作用-我收到错误消息:
ggplot_to_gtable(x)中的错误:参数必须为“ ggplot”或“ gtable”类*
我有 7 个图,我将它们组织成 2 列,一个有 3 个图,另一个有 4 个图。我曾经plot_grid从cowplot该。结果几乎是完美的,但是,具有 3 个图的列具有更大的图。如何缩放此列以在所有图中获得相同的大小并对齐每列的第一个和最后一个图?
library(ggplot2)
library(cowplot)
Value <- seq(0,1000, by = 1000/10)
Index <- 0:10
DF <- data.frame(Index, Value)
plot1 <- ggplot(DF, aes(x = Index, y = Value)) +
geom_line(linetype = 2) +
theme(aspect.ratio = 0.5)
plot2 <- ggplot(DF, aes(x = Index, y = Value)) +
geom_line(linetype = 2) +
theme(aspect.ratio = 0.5)
plot3 <- ggplot(DF, aes(x = Index, y = Value)) +
geom_line(linetype = 2) +
theme(aspect.ratio = …Run Code Online (Sandbox Code Playgroud) 我在 Twitter 上被问到这个问题,我认为把它放在这里可能会很好。
使用 制作带标签的并排图时plot_grid(),对于单字母标签,事情按预期工作:
library(cowplot)
p1 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_density(alpha = 0.7) +
ggtitle("") + theme_minimal()
p2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_density(alpha = 0.7) +
ggtitle("") +
scale_fill_grey() + theme_minimal()
plot_grid(p1, p2, labels = c("A", "B"))
Run Code Online (Sandbox Code Playgroud)
但是,如果我们使用较长的字符串作为标签,标签会向右移动,并且它们移动得越多,字符串越长:
plot_grid(p1, p2, labels = c("Density plot in color", "In gray"))
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
免责声明:我是包的作者。在这里发布这个答案,希望它会有用。
我用牛图制作了一个网格:
library(ggplot2)
library(cowplot)
ggg1 <- ggplot(mtcars, aes(mpg,vs)) + geom_point() +
theme(axis.title.x=element_blank(),
axis.title.y=element_blank())
ggg2 <- ggplot(mtcars, aes(mpg,vs)) + geom_point() +
theme(axis.title.x=element_blank(),
axis.title.y=element_blank())
ggg3 <- ggplot(mtcars, aes(mpg,vs)) + geom_point() +
theme(axis.title.x=element_blank(),
axis.title.y=element_blank())
plot_grid(plot_grid(ggg1, ggg2, labels=c("", ""), ncol = 1), ggg3, labels=c("", ""), ncol = 2)
Run Code Online (Sandbox Code Playgroud)
导致图像
(当然,这只是一个最小的工作示例)。
现在我想要一个标题为“mpg”的 xaxis 和一个标题为“vs”的 yaxis - 每个都像这样居中:
我怎样才能用 ggplot 做到这一点。我没有尝试过add_subordraw_label或如何使用 plot_grid 自定义边距和标签设置?工作。ggplot的答案:如何将常见的 x 和 y 标签添加到绘图网格中不使用牛图。我可以用 cowplot 做到这一点吗?
重要提示:我希望能够设置标签字体的大小。
ggplot2我正在使用和包制作多面板图cowplot,但我需要更改单个图的高度。最简单地用一个例子来展示
library(ggplot2)
library(cowplot)
p1 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) +
geom_point() +
theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
legend.position = "none")
p2 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) +
geom_point() +
theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
legend.position = "none")
p3 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) +
geom_point() +
theme(axis.text.y = element_blank(),
axis.title.y = element_blank(),
legend.position = "none")
p4 <- ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将test1(用 tmap 制作的地图)与test2(用 ggplot2 制作的图)结合起来,使用cowplot::plot_grid。
library(ggplot2)
library(tmap)
library(cowplot)
library(ggplotify)
test2 <-ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point()
data("World")
test1 <- tm_shape(World) + tm_polygons("HPI")
plot_grid(test1,test2)
Run Code Online (Sandbox Code Playgroud)
tmap 似乎与cowplot 不兼容:
“在 as_grob.default(plot) 中:无法将 tmap 类的对象转换为 grob”
使用时同样的问题
tmap::tmap_arrange(test1,test2)
Run Code Online (Sandbox Code Playgroud)
“tmap_arrange(test1, test2) 中的错误:并非所有参数都是 tmap 对象”
我还尝试使用 ggplotify 中的“as.grob”等函数来转换 tmap,
test3 <- as.grob(test1)
Run Code Online (Sandbox Code Playgroud)
但运气不佳“UseMethod(“as.grob”) 中的错误:没有适用于“as.grob”的方法应用于类“tmap”的对象”
有什么建议么?
谢谢!
使用plot_grid时,如何更改背景颜色?我有以下图形,但我希望背景中的所有内容都是灰色的,并且没有高度差异.我怎么能改变这个?
这是我的图形和数据代码:
数据
set.seed(123456)
Test_1 <- round(rnorm(20,mean=35,sd=3),0)/100
Test_2 <- round(rnorm(20,mean=70,sd=3),0)/100
ei.data <- as.data.frame(cbind(Test_1,Test_2))
intercept <- as.data.frame(matrix(0,20,1))
slope <- as.data.frame(matrix(0,20,1))
data <- cbind(intercept,slope)
colnames(data) <- c("intercept","slope")
for (i in 1:nrow(ei.data)){
data[i,1] <- (ei.data[i,2]/(1-ei.data[i,1]))
data[i,2] <- ((ei.data[i,1]/(1-ei.data[i,1]))*(-1))
}
Run Code Online (Sandbox Code Playgroud)
左图
p <- ggplot(data, aes(Test_1,Test_2))+
geom_point(shape=1,size=1)+
theme_bw()+
xlab(TeX("$n_{1,i}$"))+
ylab(TeX("$t_{1,i}$"))+
scale_y_continuous(limits=c(0,1),breaks=seq(0,1,0.2))+
scale_x_continuous(limits = c(0,1),breaks=seq(0,1,0.2))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "grey92", colour = NA),
plot.background = element_rect(fill = "grey92", colour = NA),
axis.line = element_line(colour = "black"))+
theme(aspect.ratio=1)
p
Run Code Online (Sandbox Code Playgroud)
正确的情节
df …Run Code Online (Sandbox Code Playgroud)