使用ggplot在多个图中使用相同宽度的y标签
Same width of y labels in multiple plots with ggplot
我想在不同的页面上使用 ggplot 生成 x 图的输出。 y 标签是具有不同长度的文本变量。
取决于最大。这些标签的长度图看起来不一样,标签和图表区域由 ggplot 自动优化。
是否应该有一种方法可以使 y 标签的宽度始终相同?
ggplot命令的代码片段:
print(ggplot(dfPriceArt[ixStart:ixEnde, ], aes(x=reorder(BATArtikelKomplett, MaxEinzelpreisPerc), y=MaxEinzelpreisPerc, size=MaxEinzelpreisPerc)) +
geom_point() +
coord_flip() +
labs(y='BATArtikel',x='MaxEinzelpreis') +
theme(axis.text=element_text(size=6)) +
scale_y_continuous(limits=c(100, maxEinzelpreisPerc)) +
scale_size_continuous(limits=c(100, maxEinzelpreisPerc)))
据我所知,没有直接的方法,但您可以填充 y 标签并使用固定宽度的字体对齐绘图。由于您没有提供任何示例数据,因此这是一个简短的示例:
library(tidyverse)
library(gridExtra)
# create two data.frames with differing lengths for the labels
df1 <- tribble(~x, ~y,
1, paste0(letters[1:10], collapse = ""))
df2 <- tribble(~x, ~y,
1, paste0(letters[1:20], collapse = ""))
# find the length of the longest label
max_length <- max(str_length(df2$y))
# pad the shorter labels
df1 <- mutate(df1, y = str_pad(y, max_length, side = "left"))
# create plots using a fixed width font
p1 <- ggplot(df1, aes(x, y)) +
geom_point()+
theme(axis.text.y = element_text(family = "mono"))
p2 <- ggplot(df2, aes(x, y)) +
geom_point()+
theme(axis.text.y = element_text(family = "mono"))
# arrange them for demonstrative purposes
grid.arrange(p1, p2)
由 reprex package (v0.2.0) 创建于 2018-04-20。
你可以试试
library(tidyverse)
library(grid)
library(gridExtra)
# some data and plots
df <- iris %>%
bind_rows(iris %>% mutate(Species=paste0(Species,"_", Species)), .id = "gr") %>%
group_by(gr,Species) %>%
summarise(y=mean(Sepal.Length)) %>%
nest(-gr) %>%
mutate(plots= map(data, ~ggplot(data=.x, aes(Species, y)) +
geom_point() +
coord_flip()))
df$plots
[[1]]
[[2]]
# updating the width
gp1<- ggplot_gtable(ggplot_build(df$plots[[1]]))
gp2<- ggplot_gtable(ggplot_build( df$plots[[2]]))
maxWidth = unit.pmax(gp1$widths[2:3], gp2$widths[2:3])
gp1$widths[2:3] <- maxWidth
gp2$widths[2:3] <- maxWidth
grid.arrange(gp1)
^
grid.arrange(gp2)
最后您可以使用
保存绘图
ggsave("test1.jpeg",grid.arrange(gp1))
ggsave("test2.jpeg",grid.arrange(gp2))
这个想法是
对 left align two graph edges (ggplot) helped a lot to find an own solution additionally with 的第一条评论(最后编辑)。
正如@Jimbou 指出的那样,我希望每页有一个情节,很抱歉没有提到这一点。
这就是我的解决方案现在的样子:
library(gridExtra)
...
for (i in 1:iLast) {
...
p <- ggplot(dfPriceArt[ixStart:ixEnde, ], aes(x=reorder(BATArtikelKomplett, MaxEinzelpreisPerc), y=MaxEinzelpreisPerc, size=MaxEinzelpreisPerc)) +
geom_point() +
coord_flip() +
labs(y='BATArtikel',x='MaxEinzelpreis') +
theme(axis.text=element_text(size=6)) +
scale_y_continuous(limits=c(100, maxEinzelpreisPerc)) +
scale_size_continuous(limits=c(100, maxEinzelpreisPerc))
plot_list[[i]] <- ggplotGrob(p)
widths_list[[i]] <- plot_list[[i]]$widths[2:5]
...
}
maxWidth <- do.call(grid::unit.pmax, widths_list)
for (i in 1:iLast) {
plot_list[[i]]$widths[2:5] <- as.list(maxWidth)
grid.draw(plot_list[[i]])
if (i != iLast) {
grid.newpage()
}
}
我想在不同的页面上使用 ggplot 生成 x 图的输出。 y 标签是具有不同长度的文本变量。
取决于最大。这些标签的长度图看起来不一样,标签和图表区域由 ggplot 自动优化。
是否应该有一种方法可以使 y 标签的宽度始终相同?
ggplot命令的代码片段:
print(ggplot(dfPriceArt[ixStart:ixEnde, ], aes(x=reorder(BATArtikelKomplett, MaxEinzelpreisPerc), y=MaxEinzelpreisPerc, size=MaxEinzelpreisPerc)) +
geom_point() +
coord_flip() +
labs(y='BATArtikel',x='MaxEinzelpreis') +
theme(axis.text=element_text(size=6)) +
scale_y_continuous(limits=c(100, maxEinzelpreisPerc)) +
scale_size_continuous(limits=c(100, maxEinzelpreisPerc)))
据我所知,没有直接的方法,但您可以填充 y 标签并使用固定宽度的字体对齐绘图。由于您没有提供任何示例数据,因此这是一个简短的示例:
library(tidyverse)
library(gridExtra)
# create two data.frames with differing lengths for the labels
df1 <- tribble(~x, ~y,
1, paste0(letters[1:10], collapse = ""))
df2 <- tribble(~x, ~y,
1, paste0(letters[1:20], collapse = ""))
# find the length of the longest label
max_length <- max(str_length(df2$y))
# pad the shorter labels
df1 <- mutate(df1, y = str_pad(y, max_length, side = "left"))
# create plots using a fixed width font
p1 <- ggplot(df1, aes(x, y)) +
geom_point()+
theme(axis.text.y = element_text(family = "mono"))
p2 <- ggplot(df2, aes(x, y)) +
geom_point()+
theme(axis.text.y = element_text(family = "mono"))
# arrange them for demonstrative purposes
grid.arrange(p1, p2)
由 reprex package (v0.2.0) 创建于 2018-04-20。
你可以试试
library(tidyverse)
library(grid)
library(gridExtra)
# some data and plots
df <- iris %>%
bind_rows(iris %>% mutate(Species=paste0(Species,"_", Species)), .id = "gr") %>%
group_by(gr,Species) %>%
summarise(y=mean(Sepal.Length)) %>%
nest(-gr) %>%
mutate(plots= map(data, ~ggplot(data=.x, aes(Species, y)) +
geom_point() +
coord_flip()))
df$plots
[[1]]
[[2]]
# updating the width
gp1<- ggplot_gtable(ggplot_build(df$plots[[1]]))
gp2<- ggplot_gtable(ggplot_build( df$plots[[2]]))
maxWidth = unit.pmax(gp1$widths[2:3], gp2$widths[2:3])
gp1$widths[2:3] <- maxWidth
gp2$widths[2:3] <- maxWidth
grid.arrange(gp1)
grid.arrange(gp2)
最后您可以使用
保存绘图ggsave("test1.jpeg",grid.arrange(gp1))
ggsave("test2.jpeg",grid.arrange(gp2))
这个想法是
对 left align two graph edges (ggplot) helped a lot to find an own solution additionally with
正如@Jimbou 指出的那样,我希望每页有一个情节,很抱歉没有提到这一点。
这就是我的解决方案现在的样子:
library(gridExtra)
...
for (i in 1:iLast) {
...
p <- ggplot(dfPriceArt[ixStart:ixEnde, ], aes(x=reorder(BATArtikelKomplett, MaxEinzelpreisPerc), y=MaxEinzelpreisPerc, size=MaxEinzelpreisPerc)) +
geom_point() +
coord_flip() +
labs(y='BATArtikel',x='MaxEinzelpreis') +
theme(axis.text=element_text(size=6)) +
scale_y_continuous(limits=c(100, maxEinzelpreisPerc)) +
scale_size_continuous(limits=c(100, maxEinzelpreisPerc))
plot_list[[i]] <- ggplotGrob(p)
widths_list[[i]] <- plot_list[[i]]$widths[2:5]
...
}
maxWidth <- do.call(grid::unit.pmax, widths_list)
for (i in 1:iLast) {
plot_list[[i]]$widths[2:5] <- as.list(maxWidth)
grid.draw(plot_list[[i]])
if (i != iLast) {
grid.newpage()
}
}