R 对齐多个带图例和不带图例的ggplot图形

R 对齐多个带图例和不带图例的ggplot图形,r,ggplot2,R,Ggplot2,我试着用ggplot画一个图表,比较两个变量的绝对值,并显示它们之间的比率。因为比率是无单位的,而值不是,所以我不能在同一个y轴上显示它们,所以我想垂直堆叠为两个单独的图形,x轴对齐 以下是到目前为止我得到的信息: library(ggplot2) library(dplyr) library(gridExtra) # Prepare some sample data. results <- data.frame(index=(1:20)) results$control <- 5

我试着用ggplot画一个图表,比较两个变量的绝对值,并显示它们之间的比率。因为比率是无单位的,而值不是,所以我不能在同一个y轴上显示它们,所以我想垂直堆叠为两个单独的图形,x轴对齐

以下是到目前为止我得到的信息:

library(ggplot2)
library(dplyr)
library(gridExtra)

# Prepare some sample data.
results <- data.frame(index=(1:20))
results$control <- 50 * results$index
results$value <- results$index * 50 + 2.5*results$index^2 - results$index^3 / 8
results$ratio <- results$value / results$control

# Plot absolute values
plot_values <- ggplot(results, aes(x=index)) +
  geom_point(aes(y=value, color="value")) +
  geom_point(aes(y=control, color="control"))

# Plot ratios between values
plot_ratios <- ggplot(results, aes(x=index, y=ratio)) +
  geom_point()

# Arrange the two plots above each other
grid.arrange(plot_values, plot_ratios, ncol=1, nrow=2)

库(ggplot2)
图书馆(dplyr)
图书馆(gridExtra)
#准备一些样本数据。

结果这是一个不需要显式使用网格图形的解决方案。它使用面,并隐藏“比率”的图例条目(使用来自的技术)

library(重塑2)

结果_long受到巴普蒂斯特评论的鼓舞,以下是我最后所做的:

library(ggplot2)
library(dplyr)
library(gridExtra)

# Prepare some sample data.
results <- data.frame(index=(1:20))
results$control <- 50 * results$index
results$value <- results$index * 50 + 2.5*results$index^2 - results$index^3 / 8
results$ratio <- results$value / results$control

# Plot ratios between values
plot_ratios <- ggplot(results, aes(x=index, y=ratio)) +
  geom_point()


# Plot absolute values
remove_x_axis =
  theme(
    axis.ticks.x = element_blank(),
    axis.text.x = element_blank(),
    axis.title.x = element_blank())

plot_values <- ggplot(results, aes(x=index)) +
  geom_point(aes(y=value, color="value")) +
  geom_point(aes(y=control, color="control")) +
  remove_x_axis

# Arrange the two plots above each other
grob_ratios <- ggplotGrob(plot_ratios)
grob_values <- ggplotGrob(plot_values)
legend_column <- 5
legend_width <- grob_values$widths[legend_column]
grob_ratios <- gtable_add_cols(grob_ratios, legend_width, legend_column-1)
grob_combined <- gtable:::rbind_gtable(grob_values, grob_ratios, "first")
grob_combined <- gtable_add_rows(
    grob_combined,unit(-1.2,"cm"), pos=nrow(grob_values))
grid.draw(grob_combined)

库(ggplot2)
图书馆(dplyr)
图书馆(gridExtra)
#准备一些样本数据。

结果另一种非常简单的解决方案如下:

# loading needed packages
library(ggplot2)
library(dplyr)
library(tidyr)

# Prepare some sample data
results <- data.frame(index=(1:20))
results$control <- 50 * results$index
results$value <- results$index * 50 + 2.5*results$index^2 - results$index^3 / 8
results$ratio <- results$value / results$control

# reshape into long format
long <- results %>% 
  gather(variable, value, -index) %>%
  mutate(facet = ifelse(variable=="ratio", "ratio", "values"))
long$facet <- factor(long$facet, levels=c("values", "ratio"))

# create the plot & remove facet labels with theme() elements
ggplot(long, aes(x=index, y=value, colour=variable)) +
  geom_point() +
  facet_grid(facet ~ ., scales="free_y") +
  scale_colour_manual(breaks=c("control","value"), values=c("green", "red", "blue")) +
  theme(axis.title.y=element_blank(), strip.text=element_blank(), strip.background=element_blank())
#加载所需的包
图书馆(GG2)
图书馆(dplyr)
图书馆(tidyr)
#准备一些样本数据
结果试试这个:

library(ggplot2)
library(gtable)
library(gridExtra)

AlignPlots <- function(...) {
  LegendWidth <- function(x) x$grobs[[8]]$grobs[[1]]$widths[[4]]

  plots.grobs <- lapply(list(...), ggplotGrob)

  max.widths <- do.call(unit.pmax, lapply(plots.grobs, "[[", "widths"))
  plots.grobs.eq.widths <- lapply(plots.grobs, function(x) {
    x$widths <- max.widths
    x
  })

  legends.widths <- lapply(plots.grobs, LegendWidth)
  max.legends.width <- do.call(max, legends.widths)
  plots.grobs.eq.widths.aligned <- lapply(plots.grobs.eq.widths, function(x) {
    if (is.gtable(x$grobs[[8]])) {
      x$grobs[[8]] <- gtable_add_cols(x$grobs[[8]],
                                      unit(abs(diff(c(LegendWidth(x),
                                                      max.legends.width))),
                                           "mm"))
    }
    x
  })

  plots.grobs.eq.widths.aligned
}

df <- data.frame(x = c(1:5, 1:5),
                 y = c(1:5, seq.int(5,1)),
                 type = factor(c(rep_len("t1", 5), rep_len("t2", 5))))

p1.1 <- ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar()
p1.2 <- ggplot(df, aes(x = x, y = y, colour = type)) + geom_line()
plots1 <- AlignPlots(p1.1, p1.2)
do.call(grid.arrange, plots1)

p2.1 <- ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar()
p2.2 <- ggplot(df, aes(x = x, y = y)) + geom_line()
plots2 <- AlignPlots(p2.1, p2.2)
do.call(grid.arrange, plots2)
库(ggplot2)
图书馆(gtable)
图书馆(gridExtra)

我会使用rbindgtable方法,但正如您所注意到的,您需要使这些表具有相同的col数。在gtable_add_cols中,为什么在创建刻面情节时不删除刻面标题?请参阅我的答案以获取示例。谢谢。这对我来说非常有效,有一个2x2的绘图数组,其中一个绘图没有图例。
library(ggplot2)
library(gtable)
library(gridExtra)

AlignPlots <- function(...) {
  LegendWidth <- function(x) x$grobs[[8]]$grobs[[1]]$widths[[4]]

  plots.grobs <- lapply(list(...), ggplotGrob)

  max.widths <- do.call(unit.pmax, lapply(plots.grobs, "[[", "widths"))
  plots.grobs.eq.widths <- lapply(plots.grobs, function(x) {
    x$widths <- max.widths
    x
  })

  legends.widths <- lapply(plots.grobs, LegendWidth)
  max.legends.width <- do.call(max, legends.widths)
  plots.grobs.eq.widths.aligned <- lapply(plots.grobs.eq.widths, function(x) {
    if (is.gtable(x$grobs[[8]])) {
      x$grobs[[8]] <- gtable_add_cols(x$grobs[[8]],
                                      unit(abs(diff(c(LegendWidth(x),
                                                      max.legends.width))),
                                           "mm"))
    }
    x
  })

  plots.grobs.eq.widths.aligned
}

df <- data.frame(x = c(1:5, 1:5),
                 y = c(1:5, seq.int(5,1)),
                 type = factor(c(rep_len("t1", 5), rep_len("t2", 5))))

p1.1 <- ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar()
p1.2 <- ggplot(df, aes(x = x, y = y, colour = type)) + geom_line()
plots1 <- AlignPlots(p1.1, p1.2)
do.call(grid.arrange, plots1)

p2.1 <- ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar()
p2.2 <- ggplot(df, aes(x = x, y = y)) + geom_line()
plots2 <- AlignPlots(p2.1, p2.2)
do.call(grid.arrange, plots2)