Dark_Clark Dark_Clark - 1 month ago 14
R Question

geom_bar tied exactly to x and y axis (without aggregating)

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')
but it produce unexpected to me result - as I figured out it sums all the y-values of corresponding to bar x-value. When tried with
stat_identity(geom='bar')
the result was nice, but one problem occured: I can't figure out how to set fixed
alpha
for
stat_identity
(seems like it is automatically binds to the number of samples with corresponding to bar x-value).
Here are the examples:

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


enter image description here

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


enter image description here

So, once again, the goal: to plot a bar-chart with x tied to "x" and y tied to "y". Hence the second example solves it, but there is an issue with
alpha
parameter.

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

enter image description here

Comments