0

With this data, the following code in R (ggplot2):

vars <- read.csv(file="c:/R/rr.csv", header=TRUE, sep=",")
str(vars)

ggplot(vars , aes(x=B, y=ï..A,  group = C, fill = C))+
geom_bar(stat="identity", show.legend = F, position=position_stack(0.5)) +
geom_text(aes(label=ï..A), position = position_stack(0.5))+
xlab("year") + ylab("Total")

Produces this plot: Barplot How do I sum values that came from the same category, e.g., 500 and 500 are from the same category, I think they should add up to show 1000 in the bar.

Nip
  • 387
  • 4
  • 11
  • 1
    Please provide a reproducible example of your dataset: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Mar 20 '20 at 22:58
  • Edited my question according to your commentary. – Nip Mar 20 '20 at 23:28
  • 1
    Instead of providing a link to your full dataset host on a google drive, it is better practice to provide a small reproducible example of your dataset as code lines that can be easily copy/paste for every users and will stay on SO. Something like the output of `dput(head(df,20))` will have been enough for users to understand your issue. – dc37 Mar 21 '20 at 00:01

1 Answers1

1

You can sum values of the variable A for each combination of variables B and C in order to have one values per category per x label.

To do that, you can use dplyr package and its functions group_by and summarise as follow:

library(dplyr)
library(ggplot2)

df %>% group_by(B,C) %>% summarise(SumA = sum(A)) %>%
  ggplot(aes(x = B, y = SumA, fill = C))+
  geom_col(show.legend = FALSE)+
  geom_text(aes(label = SumA), position = position_stack(vjust = 0.5))

enter image description here


EDIT: Adding total sum on top of each bar

You can create two new dataframes, one for the sum of A in function of B and C and one for the sum of A in function of B and used each of them as follow:

df_sumA <- df %>% group_by(B,C) %>% summarise(SumA = sum(A))
df_SumTotal <- df %>% group_by(B) %>% summarise(SumTotal = sum(A))

ggplot(df_sumA, aes(x = as.factor(B), y = SumA, fill = C))+
  geom_col(show.legend = FALSE)+
  geom_text(aes(label = SumA), position = position_stack(vjust = 0.5))+
  geom_text(inherit.aes = FALSE, data = df_SumTotal, 
            aes(x = as.factor(B),y = SumTotal, label = SumTotal), vjust = -1, color = "red")+
  ylim(0,max(df_SumTotal$SumTotal+50))

enter image description here

Does it answer your question ?

dc37
  • 15,840
  • 4
  • 15
  • 32
  • It works! Could you please show how would be the code to write a total sum on top of each bar? – Nip Mar 20 '20 at 23:44
  • Got it. Thank you! – Nip Mar 20 '20 at 23:58
  • Ok, I tried to do it by myself, but I failed. How can I sort/order your second graph? I'm using aes(x = reorder()) – Nip Mar 21 '20 at 02:24
  • 1
    Sorry, what did you try to order. If you want to order your bargraph, it's a different question. – dc37 Mar 21 '20 at 02:30
  • I just replaced 2 lines, fourth and eighth, with "ggplot(df_sumA, aes(x = reorder(B,SumA), y = SumA, fill = C))+" and "aes(x = x = reorder(B,SumA),y = SumTotal, label = SumTotal), vjust = -1, color = "red")+", respectively. – Nip Mar 21 '20 at 02:42
  • 1
    Keep only the first part. The second part won't work because `df_SumTotal` does not have variables `SumA`. Only pass `ggplot(df_sumA, aes(x = reorder(B,SumA) ... ` . Is it what you are looking for ? – dc37 Mar 21 '20 at 03:09
  • Professor. Is there a way to both parts? I mean exactly as in the example above, but orderer by the total (red numbers). – Nip Mar 21 '20 at 03:20
  • 1
    Sorry, I don't understand what you are trying to get. Please post a new question describing carefully what you are trying to get and providing a reproducible example of your dataset – dc37 Mar 21 '20 at 03:21
  • After several tries, I managed to do it! I needed to use `ggplot(df_sumA, aes(x = reorder(B,SumA, sum) ... `. Notice the `sum`. – Nip Mar 21 '20 at 04:42
  • 1
    Glad you figure it out the solution to your issue. Not sure the logic behind your solution but if it works. Great ! – dc37 Mar 21 '20 at 04:45