Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

r - Create a bar graph with pre-summarized data using ggplot2

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

enter image description here

Is that what you're after?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...