【发布时间】:2014-06-28 04:13:38
【问题描述】:
我在ggplot 中有一个情节,它向我展示了“银行”和“系统”中每个“评级”类别的价格水平。这是我的代码:
##fict
a<-c("rating1","rating2","rating3")
b<-c(1.2,1.2,1.3)
c<-c(1.6,1.4,1.6)
gg<-cbind('rating'=rep(a,2),'price'=c(b,c),'tipo'=rep(c("bank","sistem"),3))
gg<-as.data.frame(gg)
a<-rgb(red=150, green=191, blue=37, maxColorValue = 255)
b<-rgb(red=80, green=113, blue=14, maxColorValue = 255)
ggplot(gg, aes(x=tipo, y=price,width=1)) +
geom_bar(position='stack', stat='identity', fill=c(b,a), color='black') +
facet_wrap( ~ rating)+
theme_bw() + theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
strip.background = element_rect(colour = 'white', fill = 'white', size = 3),
axis.title.y=element_text(vjust=0.19),
axis.title.x=element_text(vjust=0.19)
#strip.text.x = element_text(colour = 'red', angle = 45, size = 10, hjust = 0.5, vjust = 0.5, face = 'bold')
) + xlab("My x label") +
ylab("My y label") +
labs(title = 'difference')
这段代码生成了我的情节。
我想改变 3 件事:
- 我希望标签等级显示在底部
- 我希望“银行”和“系统”标签消失,并使用带有银行和系统颜色的图例更改它。
- 如果可能,也可以将图例水平放置在 x 轴标题下
谢谢
【问题讨论】:
-
您可以在不分面的情况下完成大部分操作。试试
ggplot(gg, aes(x=rating, y=price, fill=tipo)) + geom_bar(position='dodge', stat='identity', color='black')。您可以使用 themelegend.position更改图例位置