[英]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.我有一个上传到 R 中的 .csv 文件,它大约有 2000 行。 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.一列(原因)有 6 个不同的原因,还有另一列(分钟),所以每一行都有一个原因和分钟数。 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:基础 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):您也可以为此使用 dplyr(当您计划在将来更多地使用 R 时进入 dplyr 很有用):
df <- df %>% group_by(Causes) %>% summarise_all(funs(sum))
Assuming the names of your columns are 'Causes' and 'Minutes'假设您的列的名称是“原因”和“分钟”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.