简体   繁体   English

如何针对分类变量的出现频率绘制 R 中 table() 输出的图形?

[英]How to plot a graph for the output of table() in R for Categorical variables against their occurring frequencies?

library(gapminder)
a <- table(gapminder$continent)
a
#  Africa Americas     Asia   Europe  Oceania 
#     624      300      396      360       24

How do I plot a histogram in R for the below table where categorical should be in the X-axis and frequencies should be on the y-axis?如何在 R 中为下表绘制直方图,其中分类应在 X 轴上,频率应在 y 轴上?

There is a plot method for a table so you can simply do the following:有一个表格的plot方法,因此您可以简单地执行以下操作:

library(gapminder)
a <- table(gapminder$continent)
plot(a)

在此处输入图片说明

Or you could plot it as a barplot:或者您可以将其绘制为条形图:

barplot(a)

在此处输入图片说明

Wrap a in as.data.frame and then plot.a包裹在as.data.frame ,然后进行绘图。

library(ggplot2)
library(gapminder)
a <- as.data.frame(table(gapminder$continent))
ggplot(a, aes(Var1, Freq)) + geom_col()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM