1

I'm sure this is covered somewhere by a post, but I couldn't find it. The problem is that I want to plot a single variable's values for each group in my dataset.

This is my data:

my.data <- data.frame(
  a=c("aa","aa","bb","bb","cc","cc"),
  b=c(1,1,4,4,9,9)
)

Using {ggplot2}, I tried this:

ggplot(my.data, aes(x=a, y=b)) + geom_bar(stat="identity")

However, judging from the resulting plot, I only get the sum of the values per group instead of the value itself. So what this yields is 2 (for "aa"), 8 (for "bb") and 18 (for "cc").

I also tinkered around with group- and mean() commands, but it didn't work. Does anyone know a quick fix?

0

1 Answer 1

4

Two options that come to my mind:

1) call unique on my.data

ggplot(unique(my.data), aes(x=a, y=b)) + 
  geom_bar(stat="identity")

2) use stat_summary which "operates on unique x"

ggplot(my.data, aes(x=a, y=b)) + 
  stat_summary(geom = "bar", fun.y = 'identity')

Result

enter image description here

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

1 Comment

Both solutions work perfectly for my example data, though only the latter works for my real data (for some reason). Happy that I got my results but now I'll have to figure out why solution #1 didn't work. ;-)

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.