Dark_Clark - 8 months ago 46

R Question

I have a data frame with cols "x", "y" and ordinary rows ("x" and "y" can be treated as collections of numbers) . All I want is to plot a bar-chart with x tied to "x" and y tied to "y". Tried to use

`geom_bar(stat='identity')`

`stat_identity(geom='bar')`

`alpha`

`stat_identity`

Here are the examples:

`ggplot() +`

geom_bar(data = xs, aes(x, y), stat = "identity", alpha = 0.5)

`ggplot() +`

stat_identity(data = xs, aes(x, y), geom = "bar", alpha = 0.5)

So, once again, the

`alpha`

UPDATE: File with test data can be found here. Or as GitHub gist there.

Answer

The problem is that you want to do a `bar_plot`

for data that contains multiple observations in `y`

for a single `x`

.

When using `geom_bar`

it is simply summing (binning) the values.

When you use `stat_identity`

it doesn't sum but it keeps the information of the number of occurrences for each values. Hence the alpha set to a much lower values for the extreme values (0 and 10 only have a single observation each).

So if you want to have a graph like the one you obtained with `stat_idendity`

, you need to drop the information regarding the number of occurences. It's very easy since you only have repetition of a single value.

```
ggplot(data = xs[!duplicated(xs),], aes(x=x, y=y)) +
geom_bar(stat = "identity")
```

Source (Stackoverflow)