简体   繁体   中英

bar plot with aggregated data in R

I am new to and have a question, I have a dataset having :

name surname year_of_birth monthly_salary
a     a        1970        2540
b     b        1971        5730
c     c        1970        5400
d     d        1972        4350
e     e        1973        6283
f     f        1971        4358
g     g        1973        7834
h     h        1974        4987

I would like to draw a having mean of yearly salary and data to be selected via every two years (1970&1971) (1972&1973) etc... What is the best way to do that?

I created a subset of data with the years, and calculated the means, but is there a possibility to create one bar plot with all those subsets? Or it should be done differently?

Appreciate any help or suggestion.

You should be able to do this with aggregate and barplot . I added a column to the data to specify your two-year intervals. You can define these as needed, though.

> year <- c(1970,1971,1970,1972,1973,1971,1973,1974)
> salary <- c(2540,5730,5400,4350,6283,4358,7834,4987)
> x <-data.frame(year, salary)
> x$yearlabel <- paste(2*floor(year/2), 2*floor(year/2)+1, sep = "-")
> x
  year salary yearlabel
1 1970   2540 1970-1971
2 1971   5730 1970-1971
3 1970   5400 1970-1971
4 1972   4350 1972-1973
5 1973   6283 1972-1973
6 1971   4358 1970-1971
7 1973   7834 1972-1973
8 1974   4987 1974-1975
> x2 <- aggregate(x$salary, list(x$yearlabel), mean)
> x2
    Group.1        x
1 1970-1971 4507.000
2 1972-1973 6155.667
3 1974-1975 4987.000
> barplot(x2$x, names.arg = x2$Group.1)

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