1
votes

I have a dataset called fruits_by_number like this:

category   Number
Apple     4
Banana    5 
Orange    2
Pear      0

I want to make a graph that on the x axis has bars symbolizing the fruit category and on the y axis the count as a height.

I have tried this:

qplot(x = category , y = number, data = fruits_by_number, geom = "dot")

My first question is: How instead of "dot" I ask for a bar?

My second questions is: Even if I get a bar, since in the (actual) dataset the fruit names are about 30, underneath the x-axis legend I get a big mess.

I saw this:

qplot(data = movies_df,Runtime,fill = Genre,bins = 30)

On this website there is this image:

this image

which is something close to what I want to get.

I tried to substitute fill with dodge, but it did not work.

Could you help me on how to make a graph that the fruit categories will be arranged by color on the right (like in the image) and then I will have colorfur non-overlappping bars with height equal to the number?

PS1: If I follow this recommendation:

p <- ggplot(data = trans_units_by_subject, aes(x = category, y = sumUnits_t, fill = category))
p + geom_bar(stat = "identity")

I still get underneath the x-axis the descriptions like this: enter image description here

3
If you have 30 fruits, color will not work to distinguish them. Anything more than about 8 colors is very hard to tell apart. - Gregor Thomas

3 Answers

3
votes

I would recommend you use ggplot() instead of qplot(). I believe it's more manageable and you can play better with the options of the plot. Try something like this:

p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p + geom_bar(stat = "identity")

This will produce a barplot in which the X-axis will be the fruit category, Y-axis will be the number and you will have a color fill for each fruit in the plot, including its legend. I have to say to you that it's kind of redundant to use a color filling in this kind of graphs (excluding any aesthetic function) because all the fruits will have labels in the X-axis.

Another thing, you use "dodge" when you have two factors and several levels in each of them. For example suposse that you have an extra factor "Age" in your data frame: Apple (New/Old), Pear (New/Old). So you can use something like this:

p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = Age))
p + geom_bar(stat = "identity", position = "dodge")

The plot produced will have only two colors for factor Age(New/Old), and the bars will be separated. In the example that you put, the default option is position = "stack", so you will have stacked bars.

If you still want to use colors and not having the x-axis labels, use something like this:

p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p <- p + geom_bar(stat = "identity") 
p <- p + theme(axis.text.x = element_blank())
p

And try different color palettes

Check this pages out:

http://ggplot2.tidyverse.org/reference/geom_bar.html http://ggplot2.tidyverse.org/reference/theme.html

2
votes

I suggest turning your plot sideways to accommodate the 30 categories. Using your data above,

require(ggplot2)

ggplot(fruits_by_number) + 
  geom_bar(aes(x = category, 
               y = Number, 
               fill = category), 
           stat = "identity") + 
  coord_flip()

where the above fill adds colors to the bars (remove this to make them all the same color), and the data is,

fruits_by_number <- 
read.table(text='
category   Number
Apple     4
Banana    5 
Orange    2
Pear      0', 
header=T)
1
votes

Here is a possible solution using a built-in dataset:

mtcars %>%
mutate(CARS = factor(1:32)) %>%
ggplot(aes(CARS,mpg, fill = CARS)) +
geom_bar(stat = "identity")

enter image description here