用 R 中的图替换条形图轴标签?

发布于 2025-01-17 08:08:21 字数 1867 浏览 3 评论 0原文

不久前,我问了这个关于如何将条形图 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 的各个图阴谋...但我不确定到底该怎么做才能解决这个问题?我尝试过尝试,但到目前为止还没有成功。

A while ago I asked this question about how to replace a barplots x-axis labels with individual plots and I received an answer. However, I'm back trying to do this again, except this time I want to flip the barplot. The issue I'm having is I cant figure out how to adapt the code in the previous answer to allow me to flip the plot.

For example, if I create some data and a barplot with the x-axis labels replaced by plots like so:

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))

That creates this:
barplot with plots on x-axis

Now, if I add in the line bp + coordflip() while creating the barplot and continue with the rest of the code, the barplot is flipped, but the individual plots remain in place, like so:
flipped barplot

I'm guessing I need to alter the p_axis part of the code to fix the individual plots where A, B, C are shown in the above plot... but im not sure exactly what to do to fix this? I tried experimenting but have been unsuccessful so far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我要还你自由 2025-01-24 08:08:21

我刚刚将 annotation_custom 中的 xminxmam 更改为 yminymax。另外,我将 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

需要对宽度进行一些微调。这是现在的样子。

example

I just changed in annotation_custom the xmin and xmam to ymin and ymax. Also, I changed the part bp / p_axis to 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

Some fine-tuning of the widths are needed. Here is what it looks like now.

example

忱杏 2025-01-24 08:08:21

此时,一种更简单的方法是使用 ggExtra 包,它有一个函数 ggMarginal() ,可以使用您选择的几何图形添加这些图。

请参阅https://geeksforgeeks.org/r-ggplot2-marginal-plots/一个很好的演示

A simpler approach at this point is to use the ggExtra package, which has a function ggMarginal() that adds these plots with your choice of geom.

See https://geeksforgeeks.org/r-ggplot2-marginal-plots/ for a nice demonstration

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文