ggplot2 coord_flip()与geom_text [英] ggplot2 coord_flip() with geom_text

查看:246
本文介绍了ggplot2 coord_flip()与geom_text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种轻松翻转用geom_text()注释的情节的方法。问题是,如果我使用coord_flip(),标签是不会翻转的。



一个简单的例子,如果我翻转这样的情节:

  df < -  count(diamonds,cut)

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

通过添加coord_flip(),

获得:



我知道在某些情况下这可能是一种理想的行为,但我不需要那些标签,保持与酒吧对齐,看起来更像是:





有没有解决方法?

解决方案< ,将其设置为固定值。

  ggplot(df,aes(x = cut,y = $,$标签= n))+ 
geom_bar(stat =identity)+
geom_text(angle = 270)+
coord_flip()


I'm trying to find a way to easily flip a plot annotated with geom_text(). The problem is that if I use coord_flip(), the labels are kind of not flipped.

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

by adding coord_flip(), I'm getting:

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.

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

这篇关于ggplot2 coord_flip()与geom_text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆