kostofliving8
kostofliving8

Reputation: 11

How do you combine values within several rows with the same column value?

If I have a data frame that looks like

Name     # of apples
A        1
B        2
A        3

If I am trying to make a barplot with the names (A,B) on the x axis and # of apples on the y axis, how would I combine the values for the two rows with name A so the graph would look like this?

4 x
3 x
2 x  x
1 x  x
  A  B

My code currently looks like :

ggplot(dataframe, aes(unique(dataframe$Name),_____)) + geom_bar()

And I'm trying to figure out what should go in _____ so that I can get the barplot above.

Upvotes: 1

Views: 37

Answers (1)

akrun
akrun

Reputation: 886938

We can use stat

library(ggplot2)
ggplot(df1, aes(x = Name, y = `# of apples`)) + 
          geom_bar(stat = 'sum')

-output

enter image description here

data

df1 <- structure(list(Name = c("A", "B", "A"), `# of apples` = 1:3), 
   class = "data.frame", row.names = c(NA, 
-3L))

Upvotes: 1

Related Questions