mpe
mpe

Reputation: 1000

How can I remove the bar segment outlines from a bar chart?

I have this rather large data frame that I would like to create faceted bar plots from. It all works, but since the bar heights are stacked from many individual values, the bars are no longer a solid color, but dominated by the segment outline color.

The data frame looks like this:

> 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

I want to plot the sum of bppmbp for each unit.size (unit.size goes from 1 to 51).

This is my code:

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

The result looks like this. The left screenshot is from the PDF output, the right one from the PNG output (a bit better, but you can still see the white borders).

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

From here I understand how to specify the outline color, but I have not found how to remove the outline entirely. I have tried adding color="", color=NA or color=element.empty() to geom_bar(), but none of them do.

How can I remove that outline and have solid bars? Perhaps bin all the values and just plot the bin sums? I hope there is a simpler solution.

Upvotes: 0

Views: 2056

Answers (2)

mpe
mpe

Reputation: 1000

For the record, this is how I solved my problem using aggregate():

> data.aggregate = aggregate(alldata$bppmbp, by = list(alldata$unit.size), sum)
> names(data.aggregate) = c("unit.size", "bppmbp")

This sums up the bppmbp column depending on the unit.size and gives me a much cleaner data frame that I can plot using the normal ggplot(...) + geom_bar(...):

> head(data.aggregate)
  unit.size    bppmbp
1         1  87.30581
2         2  89.60076
3         3 701.26025
4         4  94.01366
5         5  49.36587
6         6 203.07879

> ggplot(tablata, aes(x=unit.size, y=bppmbp, fill=unit.size)) +
    geom_bar(stat="identity") +
    xlab("Unit length") +
    ylab("bp/Mbp")

Bar chart without stacking artifacts

Upvotes: 0

Timo Kvamme
Timo Kvamme

Reputation: 2964

With an example data frame like this:

    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

aggregated with individual subjects, and reduce that dimension in the plot.

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

enter image description here

You get this ugly looking thing. But if you do like this

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

You get something that is aggregated over the group instead of individual cases.

enter image description here

Upvotes: 1

Related Questions