Reputation: 1025
I need ggplot2 to plot a sum of all values at specific X. What it does it just takes one value.
Excerpt of my data df
:
Day S V
1 2016-12-27 K 60
2 2016-12-27 K 600
3 2016-12-27 M 80
4 2016-12-27 M 695
Full data's str(df)
:
'data.frame': 52 obs. of 3 variables:
$ Day : POSIXlt, format: "2016-12-15" "2016-12-15" "2016-12-15" ...
$ S : chr "K" "K" "M" "M" ...
$ V : num 50 560 255 460 110 500 145 460 40 630 ...
I use following code to make a graph, but for some reason only values 600 and 695 are displayed. I would like it to display 660 and 775.
ggplot(data = daneDzien, aes(x = Day, y = V, fill = S)) +
geom_bar(stat = "identity", position = "dodge", color = "black")
I'm pretty sure it worked well for me in the past, but today I'm out of ideas what to do to make it work.
Upvotes: 3
Views: 53
Reputation: 36076
One option is to summarize the dataset outside of ggplot2, so you have one value per "S" level per date.
To work within ggplot2 instead you could summarize via stat = "summary"
with fun.y = sum
.
ggplot(df, aes(x = Day, y = V, fill = S)) +
geom_bar(stat = "summary", fun.y = sum, position = "dodge", color = "black")
Upvotes: 6