相关疑难解决方法(0)

使用facet_wrap重新排序geom_bar

我试图实现的是每个面板按给定变量排序的条形图.

一个简单的例子:

    library(ggplot2)
    library(plyr)

    df <- data.frame(fac = c("a", "b", "c", "e", "b", "c", "d"),             
                  val = c(1, 2, 7, 4, 5, 3, 1),
                  group = c(1, 1, 1, 1, 2, 2, 2))

    p1 <- ggplot(df, aes(x = fac, y = val)) + geom_bar() + facet_wrap(~ group, scales = "free") + coord_flip()
    p1

    p2 <- ggplot(df, aes(x = reorder(fac, val), y = val)) + geom_bar() + facet_wrap(~ group, scales = "free") + coord_flip()
    p2
Run Code Online (Sandbox Code Playgroud)

p2不会产生我想要的东西,因为不是每个"因子级别"都出现在所有面板中.这个问题已经有一个简单的解决方案吗?

我发现的一个解决方案如下(计算每组每个因子水平的排名).

df2 <- ddply(df, .(group), …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

9
推荐指数
1
解决办法
1695
查看次数

标签 统计

ggplot2 ×1

r ×1