如何使用ggplot绘制一列数据框?

Tra*_*ter 6 r ggplot2

我有一个这样的数据框:

 __________
|   | sums |
|---+------|
| a | 122  |
|---+------|
| b | 23   |
|---+------|
| c | 321  |
|__________|
Run Code Online (Sandbox Code Playgroud)

*注意“a”、“b”和“c”是行名称。

我想看到这样的情节:

                    ___
300 -|             |   |
200 -|  ___        |   |
100 -| |   |  ___  |   |
  0 -|_|___|_|___|_|___|______
         a     b     c
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

eip*_*i10 5

将行名添加为数据框中的一列,然后进行绘图。这是一个带有内置mtcars数据框的示例:

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))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


San*_*Dey 5

试试这个:

df
   sums
a  122
b   23
c  321

library(ggplot2)
ggplot(df, aes(x=rownames(df), sums)) + geom_bar(stat='identity')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明