在堆积条形图ggplot2上重叠的标签

时间:2016-09-01 11:06:38

标签: r ggplot2

以下代码生成适当堆叠的条形图。但是,我正在尝试添加标签以显示每个填充位置的百分比。使用下面的代码,百分比标签不会与填充边界对齐,并且所有" stack"接近0点。

这是我用过的代码

@interface RootViewController : UIViewController {
UIButton *btnCountry;
UIButton *btnState;
NSMutableArray *tempArray;
NSMutableArray *countryArray;
NSMutableArray *stateArray;
IBOutlet UITableView *tempTable;
}

@property (nonatomic,retain) UIButton *btnCountry;
@property (nonatomic,retain) UIButton *btnState;
@property (nonatomic,retain) NSMutableArray *countryArray;
@property (nonatomic,retain) NSMutableArray *stateArray;
@property (nonatomic,retain) NSMutableArray *tempArray;
@property (nonatomic,retain) UITableView *tempTable;

- (IBAction) showState:(id)sender;
- (IBAction) showCountry:(id)sender;

enter image description here

1 个答案:

答案 0 :(得分:0)

我知道这确实很老,但是您要寻找的解决方案是将position = position_stack(vjust = 0.5)添加到您的geom_text()呼叫中。像这样:


g <- ggplot(pff, aes(x = REGION, y =(..count..)/sum(..count..), fill=Q99))
g <- g + geom_bar(position="stack") + 
  labs(title="Vote par région") +
  labs(x=" ", y=" ")+
  labs(fill="Parti Politique")+
  coord_flip()+
  # scale_fill_manual(values=cbPalette)+
  scale_y_continuous(labels = percent)+
  theme_bw()+
  theme(panel.border = element_rect(colour = "white")) + 
  geom_text(aes( label = scales::percent((..count..)/sum(..count..)),
                 y=(..count..)/sum(..count..)), stat= "count", size=3, position = position_stack(vjust = 0.5))