在ggplot中的barplot [英] barplot in ggplot
问题描述
我在使用ggplot制作barplot时遇到问题。
我尝试了qplot和gplot的不同组合,
,但我要么得到一个直方图,要么换掉我的酒吧,或者决定使用logscaling。
使用普通的绘图功能。
我会这样做:
d <-1 /(10:1)
名称(d )< -paste(id,1:10)
barplot(d)
感谢
要在ggplot2中绘制条形图,您必须使用 geom =bar
或 geom_bar
。您是否尝试过任何 ggplot2网站上的geom_bar示例?
为了让你的例子工作,请尝试以下方法: 您在示例中使用的是基本R图形的一部分,而不是 (x = 1:10,y = 1 /(10:1))
ggplot
需要一个data.frame作为输入。因此,将输入数据转换为data.frame。
geom_plot
创建条形图。在这种情况下,您可能想要告诉 ggplot
数据已经使用 stat =identity
进行汇总,因为(请注意,函数 barplot
)可以创建一个直方图。
ggplot
。)
d < - data.frame ggplot(d,aes(x,y))+ geom_bar(stat =identity)
< img src =https://i.stack.imgur.com/lCOdt.pngalt =在这里输入图片描述>
I'm having problems making a barplot using ggplot. I tried different combinations of qplot and gplot, but I either get a histogram, or it swaps my bars or it decides to use logscaling.
Using the ordinary plot functions. I would do it like
d<-1/(10:1)
names(d) <-paste("id",1:10)
barplot(d)
Thanks
To plot a bar chart in ggplot2, you have to use geom="bar"
or geom_bar
. Have you tried any of the geom_bar example on the ggplot2 website?
To get your example to work, try the following:
ggplot
needs a data.frame as input. So convert your input data into a data.frame.- map your data to aesthetics on the plot using `aes(x=x, y=y). This tells ggplot which columns in the data to map to which elements on the chart.
- Use
geom_plot
to create the bar chart. In this case, you probably want to tellggplot
that the data is already summarised usingstat="identity"
, since the default is to create a histogram.
(Note that the function barplot
that you used in your example is part of base R graphics, not ggplot
.)
The code:
d <- data.frame(x=1:10, y=1/(10:1))
ggplot(d, aes(x, y)) + geom_bar(stat="identity")
这篇关于在ggplot中的barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!