用 R 中的图替换条形图轴标签?
Replace barplot axis labels with plots in R?
不久前我问了 关于如何用单独的图替换条形图 x 轴标签的问题,我得到了答案。但是,我又回来尝试再次执行此操作,只是这次我想翻转条形图。我遇到的问题是我无法弄清楚如何调整先前答案中的代码以允许我翻转情节。
例如,如果我创建一些数据和一个条形图,并将 x 轴标签替换为如下图:
df <- data.frame(vals = c(10, 5, 18),
name = c("A", "B", "C"))
bp <- df %>%
ggplot() +
geom_bar(aes(x = name, y = vals), stat = "identity") +
xlab("") +
theme_bw() +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
# create plots to use as x-axis --------------------------------------------
p1 <- ggplot(df, aes(x = vals, y = vals)) + geom_point() + theme_bw() +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
p3 <- p2 <- p1
# turn into list of plots
myList <- list(p1, p2, p3)
# -------------------------------------------------------------------------
# attach plots to axis
width <- .9 # Default width of bars
p_axis <- ggplot(df) +
geom_blank(aes(x = name)) +
purrr::map2(myList, seq_along(myList), ~ annotation_custom(ggplotGrob(.x), xmin = .y - width / 2, xmax = .y + width / 2)) +
theme_void()
bp / p_axis + plot_layout(heights = c(4, 1))
创建这个:
现在,如果我在创建条形图时添加行 bp + coordflip()
并继续其余代码,则条形图将翻转,但各个图仍保留在原位,如下所示:
我猜我需要更改代码的 p_axis
部分以修复上图中显示 A, B, C
的各个图...但我不确定到底要做什么做什么来解决这个问题?我尝试过试验,但到目前为止还没有成功。
此时更简单的方法是使用 ggExtra 包,它有一个函数 ggMarginal()
可以将这些图添加到您选择的几何图形中。
请参阅 https://geeksforgeeks.org/r-ggplot2-marginal-plots/ 进行精彩演示
我刚刚将 annotation_custom
中的 xmin
和 xmam
更改为 ymin
和 ymax
。此外,我将 bp / p_axis
部分更改为 p_axis|bp
。
p_axis <- ggplot(df) +
geom_blank(aes(y = name)) +
purrr::map2(myList, seq_along(myList), ~ annotation_custom(ggplotGrob(.x), ymin = .y - width / 2, ymax = .y + width / 2)) +
theme_void()
p_axis|bp
需要一些 fine-tuning 的宽度。这是现在的样子。
不久前我问了
例如,如果我创建一些数据和一个条形图,并将 x 轴标签替换为如下图:
df <- data.frame(vals = c(10, 5, 18),
name = c("A", "B", "C"))
bp <- df %>%
ggplot() +
geom_bar(aes(x = name, y = vals), stat = "identity") +
xlab("") +
theme_bw() +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
# create plots to use as x-axis --------------------------------------------
p1 <- ggplot(df, aes(x = vals, y = vals)) + geom_point() + theme_bw() +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
p3 <- p2 <- p1
# turn into list of plots
myList <- list(p1, p2, p3)
# -------------------------------------------------------------------------
# attach plots to axis
width <- .9 # Default width of bars
p_axis <- ggplot(df) +
geom_blank(aes(x = name)) +
purrr::map2(myList, seq_along(myList), ~ annotation_custom(ggplotGrob(.x), xmin = .y - width / 2, xmax = .y + width / 2)) +
theme_void()
bp / p_axis + plot_layout(heights = c(4, 1))
创建这个:
现在,如果我在创建条形图时添加行 bp + coordflip()
并继续其余代码,则条形图将翻转,但各个图仍保留在原位,如下所示:
我猜我需要更改代码的 p_axis
部分以修复上图中显示 A, B, C
的各个图...但我不确定到底要做什么做什么来解决这个问题?我尝试过试验,但到目前为止还没有成功。
此时更简单的方法是使用 ggExtra 包,它有一个函数 ggMarginal()
可以将这些图添加到您选择的几何图形中。
请参阅 https://geeksforgeeks.org/r-ggplot2-marginal-plots/ 进行精彩演示
我刚刚将 annotation_custom
中的 xmin
和 xmam
更改为 ymin
和 ymax
。此外,我将 bp / p_axis
部分更改为 p_axis|bp
。
p_axis <- ggplot(df) +
geom_blank(aes(y = name)) +
purrr::map2(myList, seq_along(myList), ~ annotation_custom(ggplotGrob(.x), ymin = .y - width / 2, ymax = .y + width / 2)) +
theme_void()
p_axis|bp
需要一些 fine-tuning 的宽度。这是现在的样子。