根据另一列中的数据绘制一列中的值之和 [英] Plotting the sum of values in one column a depending on data in another column

查看:76
本文介绍了根据另一列中的数据绘制一列中的值之和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R和ggplot来自电子表格背景非常陌生,我正在寻找在数据透视图中非常容易做的事情.我想根据另一列中给定的类别来绘制给定列的值的总和.

I'm pretty new to R and to ggplot coming from a spreadsheet background and I'm looking for something very easy to do in a pivot graph. I want to plot sums of values of a given column depending of the category given in another column.

让我们举个例子:

element qty category
apples  2   Red
apples  1   Green
apples  4   Red
apples  3   Green
apples  6   Yellow

我想要一个图形,该图形根据它们的类别来绘制所有苹果,而不是根据列类别中的计数,而是根据qty列中相应值的总和来绘制.

I want a graph which plots all the apples depending on their category not by the count in the column category but by the sum of the corresponding values in the qty column.

含义是6个红色苹果,4个绿色苹果和6个黄色苹果...是否有一些简单的方法可以直接在ggplot中完成此操作,或者我是否需要预先处理数据-fi.与plyr吗?

Meaning 6 red apples 4 green apples and 6 yellow apples... Is there some short way to do this directly in ggplot or do I need to work my data beforehand - fi. with plyr ?

很抱歉,如果这个问题是菜鸟问题,但我找不到任何答案...

Sorry if this question is a noob question but I wasn't able to find any answer...

推荐答案

使Roman的注释更明确:

To make Roman's comment more explicit:

# Create the data frame
element <- rep("apples", 5)
qty <- c(2, 1, 4, 3, 6)
category <- c("Red", "Green", "Red", "Green", "Yellow")
d <- data.frame(element=element, qty=qty, category=category)

使用stat="identity"来计算ggplot

ggplot(d, aes(x=category, y=qty)) + geom_bar(stat="identity")

ggplot函数末尾缺少)"

EDIT : Missing ")" added end of the ggplot function

这篇关于根据另一列中的数据绘制一列中的值之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆