ggplot中的barplot

时间:2011-06-24 08:13:21

标签: r ggplot2

我在使用ggplot制作条形图时遇到问题。 我尝试了qplot和gplot的不同组合, 但是我要么得到直方图,要么交换我的条形图或决定使用logscaling。

使用普通的绘图功能。 我会这样做

d<-1/(10:1)
names(d) <-paste("id",1:10)
barplot(d)

由于

1 个答案:

答案 0 :(得分:15)

要在ggplot2中绘制条形图,您必须使用geom="bar"geom_bar。你有没有试过geom_bar example on the ggplot2 website

要让您的示例正常运行,请尝试以下操作:

  • ggplot需要data.frame作为输入。因此,请将输入数据转换为data.frame。
  • 使用`aes(x = x,y = y)将数据映射到绘图上的美学。这告诉ggplot数据中的哪些列映射到图表上的哪些元素。
  • 使用geom_plot创建条形图。在这种情况下,您可能希望告诉ggplot数据已使用stat="identity"汇总,因为默认情况下是创建直方图。

(请注意,您在示例中使用的函数barplot是基础R图形的一部分,而不是ggplot。)

代码:

d <- data.frame(x=1:10, y=1/(10:1))
ggplot(d, aes(x, y)) + geom_bar(stat="identity")

enter image description here