帮助理解如何使用 ggplot2 制作条形图
我正在尝试使用 ggplot2 的 bar_geom 函数,但我不明白如何使用它。我制作了一个小代码示例来展示我想要做的事情:
library(ggplot2)
# sample data
sampleData = data.frame(
v1=c('a','b','c','d','e', 'f','g', 'h', 'i','j'),
v2=c(1:10)
)
sampleData$Names = data.frame( Names = paste(sampleData$v1, sampleData$v2, sep="") )
sampleData$Values = c(1:10)
# make plot
x = sampleData$Values
y = sampleData$Names
qplot(
x, y, data = sampleData,
geom="bar"
)
我希望 sampleData$Names
位于图表的 x 轴上,标记每个条形图和 SampleData$Values
来缩放条形高度。我希望将 y 轴指定为一个范围。我意识到我不明白 ggplot2 是如何工作的,因为这个小例子不起作用,但我的另一个例子正在生成一个图,但我无法指定 y 范围,因为它认为变量是分类的。
I'm trying to use the bar_geom function of ggplot2, but I can't understand how to use it. I've made a small sample of my code to show what I am trying to do:
library(ggplot2)
# sample data
sampleData = data.frame(
v1=c('a','b','c','d','e', 'f','g', 'h', 'i','j'),
v2=c(1:10)
)
sampleData$Names = data.frame( Names = paste(sampleData$v1, sampleData$v2, sep="") )
sampleData$Values = c(1:10)
# make plot
x = sampleData$Values
y = sampleData$Names
qplot(
x, y, data = sampleData,
geom="bar"
)
I want sampleData$Names
to be on the x-axis of my graph, labeling each bar and and sampleData$Values
to scale the bar height. I want the y-axis to be specified as a range. I realize that I don't understand how ggplot2 functions as this small example does not work, yet my other example is generating a plot but I cannot specify a y-range as it considers the variables to be categorical.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
qplot
需要sampleData
数据框中的列名称,而将“Names”列设置为数据框的代码也很奇怪。以下更简单的版本有效:qplot
expects column names within thesampleData
data-frame, and your code where you set the 'Names' column to a data-frame is also strange. The following simpler version works:另一个与 pchalasani 得到的结果相同的快速绘图是
特别注意参数
stat
。Another quick plot with the same results as pchalasani got is with
Pay special attention to the argument
stat
.