用 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
的各个图阴谋...但我不确定到底该怎么做才能解决这个问题?我尝试过尝试,但到目前为止还没有成功。
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))
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚将
annotation_custom
中的xmin
和xmam
更改为ymin
和ymax
。另外,我将bp / p_axis
部分更改为p_axis|bp
。需要对宽度进行一些微调。这是现在的样子。
I just changed in
annotation_custom
thexmin
andxmam
toymin
andymax
. Also, I changed the partbp / p_axis
top_axis|bp
.Some fine-tuning of the widths are needed. Here is what it looks like now.
此时,一种更简单的方法是使用 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