我有一个密度图和一个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)
我得到警告:
警告消息:删除了包含缺失值(geom_col)的2行。
情节变成这样:
我认为最好将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))
我还能做什么? 谢谢!
答案 0 :(得分: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")
编辑:我还自由地清理了一下代码(简化了调用,消除了输入错误)。