简体   繁体   中英

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?

There is a plot method for a table so you can simply do the following:

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.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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