hhh
hhh

Reputation: 52840

Why does this ggplot only plot the grid without the values?

I am trying to plot a bar chart in ggplot but I am continuously getting only the grid. This is apparently a demonstration about the draw nothing here but I would like to understand how to get the values visible in the simplest way.

enter image description here

   library(ggplot2)
    testData<-data.frame(x=c("a","b","c","d","e","f"), y=c(10,6,9,28,10,17))
    bar <- ggplot(data=testData, aes(x=c("a","b","c","d","e","f"), y=c(10,6,9,28,10,17), fill = "#FFCC00"))

One way I can get the plots is the geom_bar

enter image description here

bar <- ggplot(data=testData, aes(x=c("a","b","c","d","e","f"), y=c(10,6,9,28,10,17), fill = "#FFCC00")) + geom_bar(stat="identity")

Why are the values not plotted on the first bar chart and how to fix it the simplest way? What is the idea behind of this way of plotting with + and what is it called?

Upvotes: 0

Views: 1064

Answers (1)

www
www

Reputation: 4224

With the ggplot2 package, calling ggplot() is only meant to call the basic grid; it's like taking out a piece of graph paper before drawing a graph. In either case, having the grid ready has nothing to do with plotting the graph. That's why running the following command will result in the empty grid in your first example:

ggplot(data=testData, aes(x=x, y=y, fill = "#FFCC00"))

It's not the same as using a function like plot() or hist(), which prep the grid and plot the data at the same time:

plot(x=x,y=y,data=testData)

hist(x=x,data=testData)

The "+" in ggplot is just a way to say that there are more arguments related to the ggplot that we want included on top of the first blank grid. That's why each line separated by a "+" is typically called a layer.

So, if we want to make a simple scatterplot, we add points on top of a grid:

testData<-data.frame(x=c(1:6), y=c(10,6,9,28,10,17))

ggplot(data=testData,aes(x=x,y=y)) +
  geom_point()

Output:

enter image description here

If we want to add lines to that scatterplot, we can just add one line of code:

ggplot(data=testData,aes(x=x,y=y)) +
  geom_point() +
  geom_line()

Output:

enter image description here

We can keep adding layers like this if we want. Just note that they will print in the order that you type them (i.e. the first few lines will be below the lines printed after them):

ggplot(data=testData,aes(x=x,y=y)) +
  geom_bar(stat="identity",fill="#00BFC4") +
  geom_point() +
  geom_line()

Output:

enter image description here

Also, note that it's recommended not to call your data multiple times within a ggplot call; that can lead to errors.

Don't use:

ggplot(data=testData, aes(x=c("a","b","c","d","e","f"), 
                      y=c(10,6,9,28,10,17), fill = "#FFCC00")) + 
  geom_bar(stat="identity")

#or

ggplot(data=testData, aes(x=testData$x, y=testData$x, fill = "#FFCC00")) + 
  geom_bar(stat="identity")

Instead use:

ggplot(data=testData, aes(x=x, y=y, fill="#FFCC00")) +
  geom_bar(stat="identity")

If you want to plot data from a data frame(s) not called within the first ggplot() line, then simply add a data argument to the "layers" that use that different data frame, like this:

ggplot(data=testData,aes(x=x,y=y)) +
  geom_bar(stat="identity",fill="#00BFC4") +
  geom_point(data=differentDf, aes(x=x,y=y)) +
  geom_line(data=differentDf, aes(x=x,y=y))

Upvotes: 5

Related Questions