使用ggplot2创建预先汇总数据的条形图 [英] Create a bar graph with pre-summarized data using ggplot2

查看:139
本文介绍了使用ggplot2创建预先汇总数据的条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为 binFile

的文件中有以下数据:

  78 1 
79 4
80 33
81 150
82 714
83 2663
84 8834
85 25679
86 63654
87 139731
88 265909
89 441686
90 639773
91 803736
92 879616
93 832181
94 682584
95 480228
96 290093
97 149299
98 65443
99 24487
100 7487
101 1835
102 402
103 59
104 10

如何根据此数据创建简单的条形图第一列是沿着x轴,并且栏的高度应该与第二栏中的对应值成比例。

到现在为止,我已经尝试了以下内容:

  binData<  -  read.table(binFile)
costBars = ggplot(binData,aes(x = V1 ,y = V2))
finalPlot2 = costBars + geom_bar()
finalPlot2

然而这些命令会产生以下错误?

  stat_bin:binwidth默认为范围/ 30。使用'binwidth = x'来调整它。 
pmin(y,0)中的错误:未找到对象'y'


解决方案

您需要的是 stat =identity



  ggplot(data = binfile,aes(x = V1,y = V2))+ geom_bar(stat =identity) 

给我这个阴谋



这就是你所追求的?


I have following data in a file called binFile

78 1
79 4
80 33
81 150
82 714
83 2663
84 8834
85 25679
86 63654
87 139731
88 265909
89 441686
90 639773
91 803736
92 879616
93 832181
94 682584
95 480228
96 290093
97 149299
98 65443
99 24487
100 7487
101 1835
102 402
103 59
104 10

How do I create a simple bar graph from this data where the first column is along the x-axis and the height of the bar should be proportional to the corresponding value in second column.

Till now, I have tried the following:

binData <- read.table("binFile")
costBars = ggplot(binData,aes(x=V1,y=V2))
finalPlot2 = costBars + geom_bar()
finalPlot2

However these commands generate the following error?

stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Error in pmin(y, 0) : object 'y' not found

解决方案

What you need is stat="identity"

For example, this line

ggplot(data=binfile, aes(x=V1, y=V2)) + geom_bar(stat="identity")

gives me this plot

Is that what you're after?

这篇关于使用ggplot2创建预先汇总数据的条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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