簡體   English   中英

在同一軸上添加兩個y軸標題

[英]Adding two y-axis titles on the same axis

我將使用來自先前問題的數據集和繪圖( Here ):

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)

它包括以下情節,這是我的問題所在:

感興趣的情節

我的問題:是否可以將y軸標簽更改為同一側的兩個不同標簽? 有人會說“負收入”,並且在y軸的底部。 另一個將說“正收入”,位於SAME y軸的上部。

我已經看到這個問題是針對不同比例(在相對側)的雙y軸提出的,但是我特別希望在同一y軸上提出此問題。 感謝任何幫助-如果可能的話,我也更願意使用ggplot2解決此問題。

您可以使用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"))

在此處輸入圖片說明

# 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)

在此處輸入圖片說明

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

# 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) 

在此處輸入圖片說明

我意識到問題中的數據僅用於說明,但是對於這樣的數據,折線圖可能更易於理解:

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") 

在此處輸入圖片說明

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

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()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM