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.

3
  • 1
    Please provide a reproducible example of your dataset: stackoverflow.com/questions/5963269/… Commented Mar 20, 2020 at 22:58
  • Edited my question according to your commentary. Commented Mar 20, 2020 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. Commented Mar 21, 2020 at 0:01

1 Answer 1

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 ?

Sign up to request clarification or add additional context in comments.

10 Comments

It works! Could you please show how would be the code to write a total sum on top of each bar?
Sorry, what did you try to order. If you want to order your bargraph, it's a different question.
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 ?
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
Glad you figure it out the solution to your issue. Not sure the logic behind your solution but if it works. Great !
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.