我在使用ggplot制作条形图时遇到问题。 我尝试了qplot和gplot的不同组合, 但是我要么得到直方图,要么交换我的条形图或决定使用logscaling。
使用普通的绘图功能。 我会这样做
d<-1/(10:1)
names(d) <-paste("id",1:10)
barplot(d)
由于
答案 0 :(得分:15)
要在ggplot2中绘制条形图,您必须使用geom="bar"
或geom_bar
。你有没有试过geom_bar example on the ggplot2 website?
要让您的示例正常运行,请尝试以下操作:
ggplot
需要data.frame作为输入。因此,请将输入数据转换为data.frame。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")