简体   繁体   中英

How to plot a table containing counts by class in R?

How do you visulize in R this kind of table, where x is the class and y the number of occurences in the class? I want to visualize the distribution but all I manage to do is a barplot (putting y into a vector), so it doesn't use the info of x. I can add the tags afterwards but is there a better way to proceed and directly use this kind of format. Plus, if I have thousands of class, how can I just plot them with larger bins that the one in the table? (example here this could be plotting just two class <50 and >50).

x   y
100 1954
90  106
80  700
70  27
60  861
50  32
40  5491
30  936
20  7364
10  408

You may use barplot

barplot(y~x, df)

Or in ggplot2

library(ggplot2)
ggplot(df, aes(x, y)) + geom_col()

If you need only two categories, you can create new column and then use aggregate .

df$class <- ifelse(df$x > 50, 'less than 50', 'higher than 50')
barplot(y~class, aggregate(y~class, df, sum))

Following @Ronak Shah said, you can make graph. For many classes, you may use cut . For example, split your data as >50 and <50,

df %>%
  mutate(grp = cut(V1,2)) %>%
  ggplot(aes(grp, V2)) + geom_col()

will make bar graph. 在此处输入图片说明

If your first column is a factor in your data, you might need to add as.numeric(first column) before that.

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