1

I am trying to make a nice plot with ggplot. However, I do not know why it is not showing data.

Here is some minimum code

dummylabels <- c("A","B","C")
dummynumbers <- c(1,2,3)
dummy_frame <- data.frame(dummylabels,dummynumbers)
p= ggplot(data=dummy_frame, aes(x =dummylabels , y = dummynumbers)) + geom_bar(fill = "blue") 
p + coord_flip() + labs(title = "Title")

I get the following error message, which I cannot make sense of

Error : Mapping a variable to y and also using stat="bin".
  With stat="bin", it will attempt to set the y value to the count of cases in each group.
  This can result in unexpected behavior and will not be allowed in a future version of ggplot2.
  If you want y to represent counts of cases, use stat="bin" and don't map a variable to y.
  If you want y to represent values in the data, use stat="identity".
  See ?geom_bar for examples. (Defunct; last used in version 0.9.2)

Why do I get this error?

1
  • Welcome to SO! The error says for itself, really :) You map to variable y => you want "raw" y-values from data, but stat = "bin" produces counts instead of "raw" data, so there's a conceptual conflict. Commented Jul 6, 2015 at 14:04

1 Answer 1

3

From the error message you got:

If you want y to represent values in the data, use stat="identity".

geom_bar expects to be used as a histogram, where it bins the data itself and calculates heights based on frequency. This is the stat="bin" behaviour, and is the default. It throws an error, as you gave it a y value too. To fix it, you want stat="identity":

p <- ggplot(data = dummy_frame, aes(x = dummylabels, y = dummynumbers)) +
            geom_bar(fill = "blue", stat = "identity") +
            coord_flip() + 
            labs(title = "Title")
p
Sign up to request clarification or add additional context in comments.

Comments

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.