将水平条与ggplot中的facet_grid组合 [英] Combining horizontal bars with facet_grid in ggplot
问题描述
我想将水平条形图与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"))
我想要这种情节:
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.
推荐答案
奇怪-也许是错误?作为快速而肮脏的解决方法,您可以使用细分:
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屋!