【问题标题】:Aligning Perfectly The X Axis Values of Density Plot and geom_col完美对齐密度图和 geom_col 的 X 轴值
【发布时间】:2019-01-03 23:50:21
【问题描述】:

我有一个密度图和一个 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")

产生这个数字:

这些数字并不完全一致。

我读到 this thread 声称问题在于定义 x 限制。

但问题是,当我将geom_col 的 x 限制定义为从 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 轴值为 1 和 1.1,即使这些值在原始绘图数据中不存在:

另外,在我设置了 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))

我还能做什么? 谢谢!

【问题讨论】:

  • 我想知道你是否可以使用ggExtra::ggMarginal 来生成这个而不必弄乱对齐?如果您将README.md 向下滚动一半,您会看到iris 的外观很好,每个边距上都有一个分层密度图。 (我没有用你的数据测试过,只是一个想法。)

标签: r ggplot2 grid alignment


【解决方案1】:

使用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")

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    相关资源
    最近更新 更多