如何从条形图中删除条形段轮廓?

时间:2015-08-27 16:09:43

标签: r plot ggplot2 bar-chart

我有这个相当大的数据框,我想从中创建切面条形图。这一切都有效,但由于条形高度是从许多单独的值堆叠而成的,因此条形不再是纯色,而是以分段轮廓颜色为主。

数据框如下所示:

> head(alldata[c("unit.size", "bppmbp")])
  unit.size     bppmbp
1         6 0.11927224
2        10 0.11430256
3         1 0.07951483
4         7 0.09442386
5        13 0.18884771
6         8 0.09939353

我想绘制每个unit.size的bppmbp总和(unit.size从1到51)。

这是我的代码:

ggplot(data) + 
    aes(x=unit.size, y=bppmbp, fill=unit.size) +
    geom_bar(stat="identity")

结果如下所示。左侧屏幕截图来自PDF输出,右侧来自PNG输出(稍微好一些,但您仍然可以看到白色边框)。

Bar plot with messed up bars (PDF) Bar plot with messed up bars (PNG)

here我了解如何指定轮廓颜色,但我还没有找到如何完全删除轮廓。我尝试将color=""color=NAcolor=element.empty()添加到geom_bar(),但都没有。{/ p>

如何删除该轮廓并使用实心条?也许bin所有的值,只是绘制bin总和?我希望有一个更简单的解决方案。

1 个答案:

答案 0 :(得分:1)

使用如下示例数据框:

    x   group subject
1  50    test       1
2  52    test       1
3  23    test       1
4  53    test       2
5  23    test       2
6  53    test       2
7  62 control       3
8  63 control       3
9  36 control       3
10 57 control       4
11 58 control       4
12 58 control       4

library(Rmisc);library(ggplot2)
dfc_subjects<- summarySE(df,measurevar = "x",groupvars = c("subject","group"))
dfc_subjects
  subject   group N    x         sd         se        ci
1       1    test 3 41.66667 16.1967075  9.3511734 40.234852
2       2    test 3 43.00000 17.3205081 10.0000000 43.026527
3       3 control 3 53.66667 15.3079500  8.8380491 38.027056
4       4 control 3 57.66667  0.5773503  0.3333333  1.434218

与个别主题汇总,并减少情节中的维度。

ggplot(dfc_subjects, aes(x=group, y=x, color=group)) +
       geom_bar(stat="identity")

enter image description here

你得到这个丑陋的东西。但如果你喜欢这个

dfc_group<- summarySE(df,measurevar = "x",groupvars = "group")
dfc_group
    group N        x       sd       se       ci
1 control 6 55.66667  9.93311 4.055175 10.42416
2    test 6 42.33333 15.01555 6.130072 15.75785

ggplot(dfc_group, aes(x=group, y=x, color=group)) +
        geom_bar(stat="identity")

您可以获得在群组中汇总的内容,而不是个别案例。

enter image description here