ggplot2中的水平条形图

ATM*_*hew 50 r ggplot2

我正在做一个水平点图(?)ggplot2,它让我考虑尝试创建一个水平的条形图.但是,我发现能够做到这一点有一些限制.

这是我的数据:

df <- data.frame(Seller=c("Ad","Rt","Ra","Mo","Ao","Do"), 
                Avg_Cost=c(5.30,3.72,2.91,2.64,1.17,1.10), Num=c(6:1))
df
str(df)
Run Code Online (Sandbox Code Playgroud)

最初,我使用以下代码生成了一个点图:

require(ggplot2)
ggplot(df, aes(x=Avg_Cost, y=reorder(Seller,Num))) + 
    geom_point(colour="black",fill="lightgreen") + 
    opts(title="Avg Cost") +
    ylab("Region") + xlab("") + ylab("") + xlim(c(0,7)) +
    opts(plot.title = theme_text(face = "bold", size=15)) +
    opts(axis.text.y = theme_text(family = "sans", face = "bold", size = 12)) +
    opts(axis.text.x = theme_text(family = "sans", face = "bold", size = 12))
Run Code Online (Sandbox Code Playgroud)

但是,我现在正在尝试创建一个水平条形图,并发现我无法这样做.我试过了coord_flip(),这也没有帮助.

ggplot(df, aes(x=Avg_Cost, y=reorder(Seller,Num))) + 
    geom_bar(colour="black",fill="lightgreen") + 
    opts(title="Avg Cost") +
    ylab("Region") + xlab("") + ylab("") + xlim(c(0,7)) +
    opts(plot.title = theme_text(face = "bold", size=15)) +
    opts(axis.text.y = theme_text(family = "sans", face = "bold", size = 12)) +
    opts(axis.text.x = theme_text(family = "sans", face = "bold", size = 12)) 
Run Code Online (Sandbox Code Playgroud)

任何人都可以提供一些如何生成水平条形图的帮助ggplot2吗?

Jus*_*tin 116

ggplot(df, aes(x=reorder(Seller, Num), y=Avg_Cost)) +
  geom_bar(stat='identity') +
  coord_flip()
Run Code Online (Sandbox Code Playgroud)

没有stat='identity'ggplot想要将您的数据聚合成计数.