【发布时间】:2012-06-12 01:30:55
【问题描述】:
我正在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)
最初,我使用以下代码生成了一个点图:
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))
但是,我现在正在尝试创建水平条形图,但发现我无法这样做。我试过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))
任何人都可以就如何在ggplot2 中生成水平条形图提供一些帮助吗?
【问题讨论】: