7

I have a data frame like this:

 __________
|   | sums |
|---+------|
| a | 122  |
|---+------|
| b | 23   |
|---+------|
| c | 321  |
|__________|

*Notice "a","b" and "c" are row names.

I would like to see a plot like this:

                    ___
300 -|             |   |
200 -|  ___        |   |
100 -| |   |  ___  |   |
  0 -|_|___|_|___|_|___|______
         a     b     c

How can I accomplish that?

0

2 Answers 2

5

Add the rownames as a column in the data frame and then plot. Here's an example with the built-in mtcars data frame:

library(tibble)
library(ggplot2)

ggplot(rownames_to_column(mtcars[1:3,], var="Model"), 
       aes(x=Model, y=mpg)) +
  geom_bar(stat="identity") +
  theme(axis.text.x=element_text(angle=-90, vjust=0.5, hjust=0))

enter image description here

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

3 Comments

OK, that works, the only problem is I have 30 variables, and the labels are all on top of each other. I found this from another SO post: ggplot(df.m, aes(value)) + geom_histogram(aes(fill=variable), position="dodge") How can I combine the two?
was ggplot(cbind(Model = rownames(mtcars), mtcars)[1:3, ], ... not sufficient?
Well, I was already in the tidyverse, so I thought I'd drop in on tibble before heading back to my own dimension.
5

Just try this:

df
   sums
a  122
b   23
c  321

library(ggplot2)
ggplot(df, aes(x=rownames(df), sums)) + geom_bar(stat='identity')

enter image description here

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.