在ggplot中将水平条与facet_grid结合在一起 [英] Combining horizonal bars with facet_grid in ggplot

查看:101
本文介绍了在ggplot中将水平条与facet_grid结合在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个水平条形图与facet_grid结合起来。因此它应该只显示某个组别出现的类别。我给你一个例子,这将更具体地表达我的愿望。

I'd like to combine a horizontal bar plot with facet_grid. Thereby it should only show categories which occur for a certain group. I give you an example here, which will express my wish more concretely.

以下是一些示例数据,以方便您:

Here's some example data for your convenience:

visual_data=data.frame(Values = 10:1, Words = c("yeah","what","is","up","and","how", "are", "things","for", "you"), group = c("a","b","a","b","a","b","a","b","a","b"))

我想要这种类型的图:

I want this type of plot:

graphic=ggplot(visual_data, aes(x=Values, y=reorder(Words,Values)))
graphic=graphic + geom_point() +
  ylab("Word") + 
  xlab("Count of occurences") +
  ggtitle("Frequency") +
  facet_grid(group~., scales = "free")
graphic

工作(如下所示):

only with bars, which does not work (s. below what exactly does not work):

graphic=ggplot(visual_data, aes(y=Values, x=reorder(Words,Values)))
graphic=graphic + geom_bar(stat="identity") + 
  ylab("Word") + 
  xlab("Count of occurences") +
  coord_flip() +
  ggtitle("Frequency") +
  facet_grid(group~., scales = "free")
graphic

区别在于纵轴显示的类别。在第一个图中,只出现在一个组中的类别,而在第二个图中,每个类别都显示所有类别。

The difference lies in the displayed categories in the vertical axis. In the first plot only categories which occur in a group appear, while in the second plot all categories appear for each category.

非常感谢您了解此解决方案并告诉我: - )

Many thanks if you know a solution to this and told me that :-)

推荐答案

奇怪 - 也许是bug?作为一种快速和肮脏的解决方法,您可以使用细分:

Strange - maybe a bug? As a quick&dirty workaround, you could use segments:

ggplot(visual_data, aes(xend=Values, x=0, y=reorder(Words, Values), yend=reorder(Words, Values))) +
  geom_segment(size=10, color="#303030") + 
  facet_grid(group~., scales = "free")

这篇关于在ggplot中将水平条与facet_grid结合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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