简体   繁体   English

ggplot2 coord_flip()与geom_text

[英]ggplot2 coord_flip() with geom_text

I'm trying to find a way to easily flip a plot annotated with geom_text(). 我正在尝试找到一种方法来轻松翻转带有geom_text()注释的图。 The problem is that if I use coord_flip(), the labels are kind of not flipped. 问题是,如果我使用coord_flip(),则标签不会翻转。

A simple example, if I flip the plot like that: 一个简单的例子,如果我像这样翻转情节:

df <- count(diamonds, cut)

ggplot(df, aes(x = cut, y = n, label = n)) +
geom_bar(stat = "identity") +
geom_text()

ggplot没有翻转

by adding coord_flip(), I'm getting: 通过添加coord_flip(),我得到: 在此处输入图片说明

I know that this could be a desired behaviour in some cases but instead of that, I need the labels on the bars to stay aligned with the bars and look more like: 我知道在某些情况下这可能是理想的行为,但是,除了这些之外,我还需要条形图上的标签与条形图保持对齐并看起来更像:

在此处输入图片说明

Is there any solution to that? 有什么解决办法吗?

You can use the angle aesthetic in geom_text , setting it to a fixed value. 您可以在geom_text使用美学angle ,将其设置为固定值。

ggplot(df, aes(x = cut, y = n, label = n)) +
    geom_bar(stat = "identity") +
    geom_text(angle = 270) +
    coord_flip()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 水平ggplot2 :: geom_violin没有coord_flip - horizontal ggplot2::geom_violin without coord_flip 如何将 ggplot2 geom_col 与 facet_grid 和 coord_flip 一起使用 - How to Use ggplot2 geom_col with facet_grid and coord_flip R:在 ggplot2 中使用 coord_flip 翻转 plot 后将轴减少到 geom_density_ridges 距离 - R: Reduce axis to geom_density_ridges distance after flipping plot with coord_flip in ggplot2 ggplot2:geom_pointrange()facet_grid()与coord_flip()和自由秤 - ggplot2: geom_pointrange() facet_grid() with coord_flip() and free scales 将ggplot2转换为plotly:coord_flip出错 - converting ggplot2 to plotly: error with coord_flip 使用coord_flip()在ggplot2条形图中的图例条目顺序 - Order of legend entries in ggplot2 barplots with coord_flip() 带有coord_flip的ggplot2中errobar的自由缩放和位置 - Free scales and position of errobar in ggplot2 with coord_flip 在 ggplot2 中将 geom_text() 或 annotate() 添加到 coord_polar plot 时的奇怪行为 - Strange behaviour when adding geom_text() or annotate() to coord_polar plot in ggplot2 如何在ggplot2中一起使用coord_carteisan和coord_flip - how to use coord_carteisan and coord_flip together in ggplot2 ggplot2:以向量指定的顺序显示间隔,并使用geom_pointrange()和coord_flip()将各个组(例如facet_wrap)分开 - ggplot2: Show intervals in order specified by a vector and separate groups (e.g. with facet_wrap) using geom_pointrange() and coord_flip()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM