首页 > 解决方案 > 完美对齐密度图和 geom_col 的 X 轴值

问题描述

我有一个密度图和一个 col 图 ( geom_col),我想根据 x 轴的值完全对齐。

geom_col 图:

#creating the data 
col_plotting <- as.data.frame(matrix(ncol = 2, nrow = 20))
col_plotting[, 1] <- seq(0.1, 1, 0.1)
col_plotting[, 2] <- c(4.914910e-03, 2.485699e-17, 7.776309e-03, 1.177328e-01,
                       9.445104e-04, 7.739012e-02, 1.529308e-01, 4.829482e-01, 
                       2.902169e+00, 7.992388e+00)

#The figure
p.col_plotting <- ggplot(col_plotting, aes(x = V1, y = V2)) +
  geom_col() +
  xlab("Scores") +
  ylab("Incidence") +
  ggtitle(label="Proportion of Incidences For Each Score") +
  theme(plot.title = element_text(size = 20),
        legend.title = element_text(face = "bold")) +
  scale_x_continuous(breaks = c(seq(0.1, 1, 0.1)), 
                     minor_breaks = NULL, 
                     labels = c(seq(0.1, 1, 0.1)), 
                     limits = NULL, 
                     position = "bottom") #setting the x axis labels

密度图:

#the data
plotting <- as.data.frame(matrix(ncol = 2, nrow = length(sample(seq(0, 1, 0.001)))))
plotting[, 1] <- sample(seq(0, 1, 0.001))
plotting[, 2] <- c(rep("Yes", 500), rep("No", nrow(plotting)-500))

#The plot
P.plotting <- ggplot(plotting, aes(V1, colour = V2, fill = V2))+
  xlab("Scores") +
  ggtitle(label = "Density plot for Desicions") +
  theme(plot.title = element_text(size = 20),
        legend.title = element_text(face = "bold"))+
  geom_density(alpha = 0.60, size = 0.9) +
  scale_colour_manual(values = cbPalette, name = "Desicion") +
  scale_fill_manual(values = cbPalette, name = "Desicion")  

在使用 cowplot 时将它们对齐

plot_grid(p.col_plotting, P.plotting, 
          labels = c("A", "B"), 
          nrow = 2, align = "v", axis = "lr")

产生这个数字:

在此处输入图像描述

这些数字彼此并不完全对齐。

我读 了这个线程,声称问题在于定义 x 限制。

但问题是,当我将 x 的限制定义geom_col为从 0 到 1 时(因为它在数据框中):

p.col_plotting <- ggplot(col_plotting, aes(x = V1, y = V2))+
  geom_col() +
  xlab("Scores") +
  ylab("Incidence") +
  ggtitle(label = "Proportion of Incidences For Each Score") +
  theme(plot.title = element_text(size = 20),
        legend.title = element_text(face = "bold")) +
  scale_x_continuous(breaks = c(seq(0.1, 1, 0.1)), 
                     minor_breaks = NULL, 
                     labels = c(seq(0.1, 1, 0.1)), 
                     limits = NULL, 
                     position = "bottom") +
  xlim(0, 1)

我收到警告:

警告消息:删除了 2 行包含缺失值 (geom_col)。

情节是这样的:

在此处输入图像描述

我认为最好将 col 图和密度图的 x 限制(0, 1.1)定义geom_col为尽管这些值在原始绘图数据中不存在:

在此处输入图像描述

此外,在我设置了 x 限制值之后,对齐仍然不完美。

接下来,我还尝试了另外两种解决方案:

此代码在尝试使用rbind不等维度的 2 个变量时产生错误

g2 <- ggplotGrob(p.col_plotting )
g3 <- ggplotGrob(P.plotting)
g <- rbind(g2, g3, size = "first")
g$widths <- grid::unit.pmax(g2$widths, g3$widths)
grid::grid.newpage()
grid::grid.draw(g)

此代码产生了相同的未对齐网格

g1 <- ggplotGrob(p.col_plotting)
g2 <- ggplotGrob(P.plotting)
colnames(g1) <- paste0(seq_len(ncol(g1)))
colnames(g2) <- paste0(seq_len(ncol(g2)))
x11()
grid::grid.draw(gridExtra::gtable_combine(g2, g1, along=2))

我还可以做些什么?谢谢!

标签: rggplot2gridalignment

解决方案


使用coord_cartesian(xlim = c(min, max)). 它在不丢弃数据的情况下设置限制:

library(ggplot2)
library(cowplot)

# Color-blind friendly colors
# (you forgot to add this variable)
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", 
               "#F0E442", "#0072B2", "#D55E00", "#CC79A7")


# Plot 1
p.col_plotting <- ggplot(col_plotting, aes(x = V1, y = V2)) +
    geom_col() +
    labs(title = "Proportion of Incidences for Each Score",
         x = "Scores", 
         y = "Incidence") +
    theme(plot.title = element_text(size = 20),
          legend.title = element_text(face = "bold")) +
    scale_x_continuous(breaks = c(seq(0, 1, 0.1))) +
    coord_cartesian(xlim = c(seq(0, 1, 0.1)))

# Plot 2
p.plotting <- ggplot(plotting, aes(V1, colour = V2, fill = V2)) +
    labs(title = "Density Plot for Decisions", 
         x = "Scores",
         y = "Density") +
    theme(plot.title = element_text(size = 20),
          legend.title = element_text(face = "bold")) +
    geom_density(alpha = 0.60, size = 0.9) +
    scale_colour_manual(values = cbPalette, name = "Decision") +
    scale_fill_manual(values = cbPalette, name = "Decision")


# Plot Grid
plot_grid(p.col_plotting, p.plotting, labels = c("A", "B"), 
          nrow = 2, align = "v", axis = "lr")

1

编辑:我还冒昧地清理了您的代码(简化调用,删除拼写错误)。


推荐阅读