Jwan622
Jwan622

Reputation: 11659

How to see the distribution of a column in R?

I have a dataset that looks like this:

A tibble: 935 x 17
    wage hours    iq   kww  educ exper tenure   age married  black  south  urban  sibs brthord meduc
   <int> <int> <int> <int> <int> <int>  <int> <int>  <fctr> <fctr> <fctr> <fctr> <int>   <int> <int>
 1   769    40    93    35    12    11      2    31       1      0      0      1     1       2     8
 2   808    50   119    41    18    11     16    37       1      0      0      1     1      NA    14
 3   825    40   108    46    14    11      9    33       1      0      0      1     1       2    14
 4   650    40    96    32    12    13      7    32       1      0      0      1     4       3    12
 5   562    40    74    27    11    14      5    34       1      0      0      1    10       6     6
 6  1400    40   116    43    16    14      2    35       1      1      0      1     1       2     8
 7   600    40    91    24    10    13      0    30       0      0      0      1     1       2     8
 8  1081    40   114    50    18     8     14    38       1      0      0      1     2       3     8
 9  1154    45   111    37    15    13      1    36       1      0      0      0     2       3    14
10  1000    40    95    44    12    16     16    36       1      0      0      1     1       1    12
...

What can I run to see the distribution of wage (the first column). Specifically, I want to see how many people have a wage of under $300.

What ggplot function can I run?

Upvotes: 1

Views: 158

Answers (2)

M--
M--

Reputation: 29173

You can get the cumulative histogram:

library(ggplot2)
ggplot(df,aes(wage))+geom_histogram(aes(y=cumsum(..count..)))+
                     stat_bin(aes(y=cumsum(..count..)),geom="line",color="green")

If you specifically want to know the count of entries with a certain condition, in base r you can use the following:

count(df[df$wage > 1000,])

## # A tibble: 1 x 1
##       n
##   <int>
## 1     3

enter image description here

Data:

 df <- structure(list(wage = c(769L, 808L, 825L, 650L, 562L, 1400L,            
     600L, 1081L, 1154L, 1000L), hours = c(40L, 50L, 40L, 40L, 40L,            
     40L, 40L, 40L, 45L, 40L), iq = c(93L, 119L, 108L, 96L, 74L, 116L,         
     91L, 114L, 111L, 95L), kww = c(35L, 41L, 46L, 32L, 27L, 43L,              
     24L, 50L, 37L, 44L), educ = c(12L, 18L, 14L, 12L, 11L, 16L, 10L,          
     18L, 15L, 12L), exper = c(11L, 11L, 11L, 13L, 14L, 14L, 13L,              
     8L, 13L, 16L), tenure = c(2L, 16L, 9L, 7L, 5L, 2L, 0L, 14L, 1L,           
     16L), age = c(31L, 37L, 33L, 32L, 34L, 35L, 30L, 38L, 36L, 36L            
     ), married = c(1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L), black = c(0L,     
     0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L), south = c(0L, 0L, 0L, 0L,            
     0L, 0L, 0L, 0L, 0L, 0L), urban = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,            
     1L, 0L, 1L), sibs = c(1L, 1L, 1L, 4L, 10L, 1L, 1L, 2L, 2L, 1L             
     ), brthord = c(2L, NA, 2L, 3L, 6L, 2L, 2L, 3L, 3L, 1L), meduc = c(8L,     
     14L, 14L, 12L, 6L, 8L, 8L, 8L, 14L, 12L)), .Names = c("wage",             
     "hours", "iq", "kww", "educ", "exper", "tenure", "age", "married",        
     "black", "south", "urban", "sibs", "brthord", "meduc"), row.names = c(NA, 
     -10L), class = c("tbl_df", "tbl", "data.frame")) 

Upvotes: 1

Samuel
Samuel

Reputation: 3051

Try this:

library(dplyr)
library(ggplot2)
df <- df %>% filter(wage < 300)
qplot(wage, data = df)

Upvotes: 1

Related Questions