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

Time:10-14

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

CodePudding user response:

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))

CodePudding user response:

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.enter image description here

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

  • Related