Reputation: 4298
I am an absolute beginner, and I have recently started using excellent package ggplot. I have a question about using position = "identity" with bar chart. I searched through the internet and found this: http://docs.ggplot2.org/current/geom_tile.html However, they are not in relation to geom_bar()
A) First graph: (which works well)
ggplot(diamonds, aes(color, fill = cut)) +
geom_bar()
This plots frequency (y-axis) wrt color and fills based on "cut". I am good with this.
B) Now, in the second graph, I'm unsure what is happening:
ggplot(diamonds, aes(color, fill = cut)) +
geom_bar(position = "identity", alpha = 1 / 2, colour = "red")
Can someone please explain why the second graph is a little different (i.e. the height of bar graph is different in the two graphs; color scheme has also changed--I would have expected the bars to be red because I am explicitly setting colour = "red" but the bars have gradient color scheme, and they have a "red" border.
In drawing this, I am using publicly available ggplot2
library and diamond
dataset package that comes with it.
I am a beginner, so I am sorry if my question sounds too basic.
Upvotes: 3
Views: 34096
Reputation: 377
As @Richard Telford said, position="identity"
overlaps the bar, and the default option is position="stack"
as you can see with :
args(geom_bar)
function (mapping = NULL, data = NULL, stat = "count", position = "stack",
..., width = NULL, binwidth = NULL, na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE)
args
show the arguments for any function, with default values, as you can see here, the default value for position
argument is "stack", so in your first exemple, bars are stacked.
And if you want to specify the "filling" colors, you need an special extra argument : a scale (if you try fill=...
in the geom_bar
call it overwrites fill=cut
the the ggplot
call). Here an exemple with ugly colors, and black borders :
ggplot(diamonds, aes(color, fill = cut)) +
geom_bar(position = "stack", color="black") +
scale_fill_manual(values=c("red", "blue", "green", "yellow", "gray70"))
Upvotes: 5