简体   繁体   中英

How to plot factors of one column against their total sum of values from another column in R?

I have a .csv file that I uploaded into R, it has about 2000 rows. There is one column(causes) with 6 different causes, there is also another column(minutes), So each row has a cause and an amount of minutes. I'd like to plot the different causes against their summed amount of minutes. I already figured out how to plot the amount of different factors in all rows together by doing:

ggp <- ggplot(data.frame(table$cause_group), aes(x=table$cause_group)) +
  geom_bar()

Any help would be appreciated and sorry for the absolute beginner question.

Cheers!

I would aggregate the data before plotting.

Base R:

df <- aggregate(Minutes ~ Causes, sum, data = df)

you can also use dplyr for this (useful to get into dplyr when you are planning to work more with R in the future):

df <- df %>% group_by(Causes) %>% summarise_all(funs(sum))

Assuming the names of your columns are 'Causes' and 'Minutes'

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