1

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

1
  • If you have 30 fruits, color will not work to distinguish them. Anything more than about 8 colors is very hard to tell apart. Commented Dec 12, 2017 at 19:28

3 Answers 3

3

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

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

3 Comments

Thank you - You said "all the fruits will have labels in the X-axis" you are correct, but I am trying to avoid that. My categories are about 30 so on the x-axis I don't get something legible. So can I while using the legentd describing the color fill not have the category legend appearing on the bottom? I have updated my question with that image of the legend underneath x.
And the second recommendation worked great! - I just included the image in my PS1, just to show you why I did not want the x-axis labeled.
Glad it was helpful! I hope you keep using ggplot2. For future reference, remember to check the links I put at the end of my answer. You can use the theme() to change many things, including the aspect of your axis labels, font, fontsize, etc. Try this instead of the element_blank(), and you can rotate your axis labels in z degrees: theme(axis.text.x = element_text(angle = z))
2

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)

2 Comments

Very helpful thank you - the rotation really helped. I did not know it was even an option.
Sure. You can pretty much plot whatever you want with ggplot2. Here's it's online documentation for the basics: ggplot2.tidyverse.org
1

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

1 Comment

It's probably a better practice to map fill to mpg rather than CARS because coloring by a generated sequential ID provides 0 additional information.

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.