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?
%>% is a pipe operator reexported from the magrittr package. Start by reading the vignette. Adding things to a ggplot changes the object that gets created. The print method of ggplot draws an appropriate plot depending upon the contents of the variable.
The function geom_point() adds a layer of points to your plot, which creates a scatterplot.
You may notice that we sometimes reference 'ggplot2' and sometimes 'ggplot'. To clarify, 'ggplot2' is the name of the most recent version of the package. However, any time we call the function itself, it's just called 'ggplot'.
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))
Just try this:
df
sums
a 122
b 23
c 321
library(ggplot2)
ggplot(df, aes(x=rownames(df), sums)) + geom_bar(stat='identity')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With