ggplot2中的水平Barplot [英] Horizontal Barplot in ggplot2
问题描述
我正在制作 ggplot2
中的水平点图(?),这让我想到要创建一个水平barplot。然而,我发现可以做到这一点的一些限制。
以下是我的数据:
<$ p $ (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(color =black,fill =lightgreen) +
opts(title =Avg Cost)+
ylab(Region)+ xlab()+ ylab()+ xlim(c(0,7))+
opts(axis.text.y = theme_text(family =sans,face =bold,size = 12))+
opts(axis.text.x = theme_text(family =sans,face =bold,size = 12))
但是,我正在尝试创建一个横向barplot,并发现我无法这样做。我试过 coord_flip()
,但这也没有帮助。
ggplot(df,aes(x = Avg_Cost,y = reorder(Seller,Num)))+
geom_bar(color =black,fill =lightgreen)+
opts(title =平均成本)+
ylab(Region)+ xlab()+ ylab()+ xlim(c(0,7))+
opts(plot.title = theme_text )+
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
中生成水平barplot提供一些帮助吗?
ggplot(df,aes(x = reorder(Seller,Num),y = Avg_Cost))+
geom_bar(stat ='identity')+
coord_flip()
没有 stat ='identity' code> ggplot希望将您的数据汇总到计数中。
I was working on doing a horizontal dot plot (?) in ggplot2
, and it got me thinking about trying to create a horizontal barplot. However, I am finding some limitations in being able to do this.
Here is my data:
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)
Initially, I generated a dot plot using the following code:
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))
However, I am now trying to create a horizontal barplot and finding that I am unable to do so. I've tried coord_flip()
and that was not helpful either.
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))
Can anyone provide some assistance on how to generate a horizontal barplot in ggplot2
?
ggplot(df, aes(x=reorder(Seller, Num), y=Avg_Cost)) +
geom_bar(stat='identity') +
coord_flip()
Without stat='identity'
ggplot wants to aggregate your data into counts.
这篇关于ggplot2中的水平Barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!