2016-11-14 119 views
2

我要使用的數據集和情節是從以前的問題就來了(Here)新增兩個y軸標題:在同一軸線上

dat <- read.table(text = " Division Year OperatingIncome 
1 A 2012   11460 
2 B 2012   7431 
3 C 2012   -8121 
4 D 2012   15719 
5 E 2012    364 
6 A 2011   12211 
7 B 2011   6290 
8 C 2011   -2657 
9 D 2011   14657 
10 E 2011   1257 
11 A 2010   12895 
12 B 2010   5381 
13 C 2010   -2408 
14 D 2010   11849 
15 E 2010    517",header = TRUE,sep = "",row.names = 1) 

dat1 <- subset(dat,OperatingIncome >= 0) 
dat2 <- subset(dat,OperatingIncome < 0) 
ggplot() + 
    geom_bar(data = dat1, aes(x=Year, y=OperatingIncome, fill=Division),stat = "identity") + 
    geom_bar(data = dat2, aes(x=Year, y=OperatingIncome, fill=Division),stat = "identity") + 
    scale_fill_brewer(type = "seq", palette = 1) 

它包括以下情節,這就是我的問題進來:

Plot of interest

我的問題:是否有可能對我來說,y軸標籤更改爲兩個不同的標籤在同一側?人們會說「負收入」,並且處於Y軸的底部。另一個會說「正收入」,並且位於同一個Y軸的上部。

我已經看到這個問題在不同尺度(在相反側)上的雙y軸問題,但我特別希望在相同的y軸上。感謝任何幫助 - 如果可能的話,我也希望使用ggplot2來解決這個問題。

+0

所以你要在兩個地方添加文本在y軸? – rawr

+0

是的(但我也想控制每個y軸標題的開始位置)。 – EntryLevelR

+1

我想補充一點,我想讓y軸上的每個標題獨立於另一個。例如,也許我做一個藍色和一個紅色。 – EntryLevelR

回答

4

您可以使用annotate爲負收入和正收入添加標籤。要在繪圖面板外添加文本,您需要關閉裁剪。下面是內外情節面板添加文本的例子:

# Plot 
p = ggplot() + 
    geom_bar(data = dat1, aes(x=Year, y=OperatingIncome, fill=Division),stat = "identity") + 
    geom_bar(data = dat2, aes(x=Year, y=OperatingIncome, fill=Division),stat = "identity") + 
    scale_fill_brewer(type = "seq", palette = 1) + 
    geom_hline(yintercept=0, lwd=0.3, colour="grey20") + 
    scale_x_continuous(breaks=sort(unique(dat$Year))) + 
    theme_bw() 

# Annotate inside plot area 
p + coord_cartesian(xlim=range(dat$Year) + c(-0.45,0.4)) + 
    annotate(min(dat$Year) - 0.53 , y=c(-5000,5000), label=c("Negative Income","Positive Income"), 
      geom="text", angle=90, hjust=0.5, size=3, colour=c("red","blue")) 

enter image description here

# Annotate outside plot area by turning off clipping 
pp = p + coord_cartesian(xlim=range(dat$Year) + c(-0.4,0.4)) + 
    annotate(min(dat$Year) - 0.9, y=c(-6000,10000), label=c("Negative Income","Positive Income"), 
      geom="text", angle=90, hjust=0.5, size=4, colour=c("red","blue")) + 
    labs(y="") 

pp <- ggplot_gtable(ggplot_build(pp)) 
pp$layout$clip <- "off" 
grid.draw(pp) 

enter image description here

您還可以使用cowplot通過@Gregor的建議。我以前沒有嘗試過,所以也許有比我在下面做的更好的方法,但是它看起來像您必須使用視口座標而不是數據座標來放置註釋。

# Use cowplot 
library(cowplot) 

ggdraw() + 
    draw_plot(p + labs(y=""), 0,0,1,1) + 
    draw_label("Positive Income", x=0.01, y = 0.5, col="blue", size = 10, angle=90) + 
    draw_label("Negative Income", x=0.01, y = 0.15, col="red", size = 10, angle=90) 

enter image description here

我意識到在這個問題中的數據僅僅是爲了說明,但對於這樣的數據,一個線圖可能被證明更容易理解:

library(dplyr) 

ggplot(dat, aes(x=Year, y=OperatingIncome, color=Division)) + 
    geom_hline(yintercept=0, lwd=0.3, colour="grey50") + 
    geom_line(position=position_dodge(0.2), alpha=0.5) + 
    geom_text(aes(label=Division), position=position_dodge(0.2), show.legend=FALSE) + 
    scale_x_continuous(breaks=sort(unique(dat$Year))) + 
    theme_bw() + 
    guides(colour=FALSE) + 
    geom_line(data=dat %>% group_by(Year) %>% summarise(Net=sum(OperatingIncome), Division=NA), 
      aes(x=Year, y=Net), alpha=0.4) + 
    geom_text(data=dat %>% group_by(Year) %>% summarise(Net=sum(OperatingIncome), Division=NA), 
      aes(x=Year, y=Net, label="Net"), colour="black") 

enter image description here

或者,如果需要條形圖,可能是這樣的:

ggplot() + 
    geom_bar(data = dat %>% arrange(OperatingIncome) %>% 
      mutate(Division=factor(Division,levels=unique(Division))), 
      aes(x=Year, y=OperatingIncome, fill=Division), 
      stat="identity", position="dodge") + 
    geom_hline(yintercept=0, lwd=0.3, colour="grey20") + 
    theme_bw() 

enter image description here

+0

一個很好的概述!我非常感謝你的多個例子 - 深思熟慮! – EntryLevelR