如何使用ggplot2在绘图中以不同方式对正负值进行着色

时间:2016-05-24 12:50:41

标签: r ggplot2

我有以下数据:

head(df2,20)
     LAF_Date variable  value   pos
1  2001-04-03      Net  -1470 FALSE
2  2001-04-04      Net -17675 FALSE
3  2001-04-09      Net -13820 FALSE
4  2001-04-10      Net -16885 FALSE
5  2001-04-11      Net -18160 FALSE
6  2001-04-12      Net -19170 FALSE
7  2001-04-16      Net -13715 FALSE
8  2001-04-17      Net -17265 FALSE
9  2001-04-18      Net -11115 FALSE
10 2001-04-19      Net   -600 FALSE
11 2001-04-20      Net -11375 FALSE
12 2001-04-23      Net  -8200 FALSE
13 2001-04-24      Net  -5600 FALSE
14 2001-04-25      Net  -5300 FALSE
15 2001-04-26      Net      0  TRUE
16 2001-04-27      Net   -110 FALSE
17 2001-04-30      Net    410  TRUE
18 2001-05-02      Net      0  TRUE
19 2001-05-03      Net  -4000 FALSE
20 2001-05-04      Net  -5750 FALSE

我的第二个数据集:

ndtl2000
         year    ndtl  oneperc minusoneperc
1  2016-03-30 9327290 93272.90    -93272.90
2  2015-03-30 8533285 85332.85    -85332.85
3  2014-03-30 7705560 77055.60    -77055.60
4  2013-03-30 6750454 67504.54    -67504.54
5  2012-03-30 5909082 59090.82    -59090.82
6  2011-03-30 5207969 52079.69    -52079.69
7  2010-03-30 4492826 44928.26    -44928.26
8  2009-03-30 3834110 38341.10    -38341.10
9  2008-03-30 3196939 31969.39    -31969.39
10 2007-03-30 2611933 26119.33    -26119.33
11 2006-03-30 2109049 21090.49    -21090.49
12 2005-03-30 1700198 17001.98    -17001.98
13 2004-03-30 1504416 15044.16    -15044.16
14 2003-03-30 1280853 12808.53    -12808.53
15 2002-03-30 1103360 11033.60    -11033.60
16 2001-03-30  962618  9626.18     -9626.18
17 2000-03-30  813345  8133.45     -8133.45

我现在将这两个图一起绘制:

p <- ggplot() + 
  geom_line(data = df2, aes(x=LAF_Date, y=value,color = "Net"),size=1.05,stat='identity', position='identity') + labs(color="") + scale_y_continuous(breaks = seq(-300000,500000, by = 100000),labels = comma) + scale_x_date(date_breaks = "3 year",date_labels = "%Y") + xlab('\nYear') + ylab('Rs Cr\n') + labs(caption="Source:Data from RBI Website") + ggtitle("LAF Net Injections (+)/Absorption(-) in Rs Cr\n") + theme(plot.title = element_text(face="bold",size = 10,hjust = 0.5)) + theme(axis.text.x=element_text(size=7, color = "black")) + theme(axis.text.y=element_text(size=9, color = "black")) + theme(
axis.title.x = element_text(vjust=-0.70,size=9), axis.title.y = element_text(vjust=0.55,size=9)) + theme(panel.background = element_rect(fill = "#fbfefe")) + theme(plot.background = element_rect(fill="#e0edbd" )) + theme(panel.grid.minor = element_line(colour = "black", linetype = "dotted",size = 0.3)) + theme(plot.caption=element_text(size=8, hjust=1.0, margin=margin(t=10))) + 

geom_line(data = ndtl2000, aes(x=year, y=oneperc, color = "oneperc"),size=1.05) + geom_line(data = ndtl2000, aes(x=year, y=minusoneperc, color = "minusoneperc"),size=1.05) + scale_y_continuous(breaks = seq(-300000,500000, by = 100000),labels = comma) + scale_x_date(date_breaks = "3 year",date_labels = "%Y") + labs(color="") + xlab('\nYear') + ylab('Net LAF(Rs Cr)\n') + labs(caption="Source:Data from RBI Website") + theme(plot.title = element_text(face="bold",size = 10,hjust = 0.5)) + theme(axis.text.x=element_text(size=7, color = "black")) + theme(axis.text.y=element_text(size=9, color = "black")) + theme(
axis.title.x = element_text(vjust=-0.70,size=9), axis.title.y = element_text(vjust=0.55,size=9)) + theme(panel.background = element_rect(fill = "#fbfefe")) + theme(plot.background = element_rect(fill="#e0edbd" )) + theme(panel.grid.minor = element_line(colour = "black", linetype = "dotted",size = 0.3)) + theme(plot.caption=element_text(size=8, hjust=1.0, margin=margin(t=10))) + scale_color_discrete(name="",breaks=c("oneperc","minusoneperc"),labels=c("+1% of NDTL","-1% of NDTL")) + geom_hline(yintercept=0,color="black",size=0.7)

p

enter image description here

我得到了以下美丽的情节。但是为了传达更多信息,我想要的是df2数据的负值以红色显示(值低于0行),正值显示为绿色。我确实在df2中创建了一个逻辑列,其中TRUE / FALSE值,但我无法得到不同的颜色....

需要帮助....

3 个答案:

答案 0 :(得分:2)

尝试使用&#39; color = value&lt; 0&#39 ;.当然这是在qplot中,但它应该与geom函数的功能相同。

> test = rnorm(20)
> test
[1]  0.32399811 -0.08300721  0.80059717 -1.17414859  0.75542097  0.74445661     -0.72229506 -1.00234708  1.30489094  1.73012769
[11]  0.36653638  0.61772379 -1.13436348  1.40682839  1.25269803 -0.80424961 -0.38685804  1.35256767  0.01789222 -0.19187078

> qplot(1:20,test,geom='point',color=test < 0)

Plot of test data

答案 1 :(得分:1)

终于搞定了

使用geom_bar比使用geom_line

更好的绘图
p <- ggplot() + 
  geom_bar(data = df2, aes(x=LAF_Date, y=value,fill=pos),size=1.55,stat='identity', position='identity') + labs(fill="") + scale_fill_discrete(name="",breaks=c("TRUE","FALSE"),labels=c("Net Injection(+)","Net Absorption (-)")) +scale_y_continuous(breaks = seq(-300000,500000, by = 100000),labels = comma) + scale_x_date(date_breaks = "3 year",date_labels = "%Y") + xlab('\nYear') + ylab('Rs Cr\n') + labs(caption="Source:Data from RBI Website") + ggtitle("LAF Net Injections (+)/Absorption(-) in Rs Cr\n") + theme(plot.title = element_text(face="bold",size = 10,hjust = 0.5)) + theme(axis.text.x=element_text(size=7, color = "black")) + theme(axis.text.y=element_text(size=9, color = "black")) + theme(
axis.title.x = element_text(vjust=-0.70,size=9), axis.title.y = element_text(vjust=0.55,size=9)) + theme(panel.background = element_rect(fill = "#fbfefe")) + theme(plot.background = element_rect(fill="#e0edbd" )) + theme(panel.grid.minor = element_line(colour = "black", linetype = "dotted",size = 0.3)) + theme(plot.caption=element_text(size=8, hjust=1.0, margin=margin(t=10))) + 

geom_line(data = ndtl2000, aes(x=year, y=oneperc, color ="oneperc"),size=1.35) + geom_line(data = ndtl2000, aes(x=year, y=minusoneperc, color = "minusoneperc"),size=1.35) + labs(color="") + xlab('\nYear') + ylab('Net LAF(Rs Cr)\n') + labs(caption="Source:Data from RBI Website") + theme(plot.title = element_text(face="bold",size = 10,hjust = 0.5)) + theme(axis.text.x=element_text(size=7, color = "black")) + theme(axis.text.y=element_text(size=9, color = "black")) + theme(
axis.title.x = element_text(vjust=-0.70,size=9), axis.title.y = element_text(vjust=0.55,size=9)) + theme(panel.background = element_rect(fill = "#fbfefe")) + theme(plot.background = element_rect(fill="#e0edbd" )) + theme(panel.grid.minor = element_line(colour = "black", linetype = "dotted",size = 0.3)) + theme(plot.caption=element_text(size=8, hjust=1.0, margin=margin(t=10))) + scale_color_manual(name="",breaks=c("oneperc","minusoneperc"),labels=c("+1% of NDTL","-1% of NDTL"),values=c("#6238de","#ef7b18")) + geom_hline(yintercept=0,color="black",size=0.7)

p

enter image description here

答案 2 :(得分:0)

这里有两种方法可以为图的正负部分绘制不同颜色的线。在sign中定义data.frame的{​​{1}}列似乎可以与ggplot一起使用:

tdata = tibble(mx=seq(0,4*pi,length.out = 300),my=sin(mx), sign=sign(my))

tdata %>% ggplot(aes(x=mx)) +  
  geom_line(aes(y=my, color=sign))

这将产生情节:

enter image description here

第二种方法添加True / False列sign。然后,我们使用自定义函数group_transitions为每个连续的True of False分配一个标签,其背后的想法是从here借来的。然后,我们使用group美学映射让ggplot知道只有同一组中的那些点应该连接:

tdata = tibble(mx=seq(0,4*pi,length.out = 1000),my=sin(mx), sign=my < 0)

group_transitions = function(x) c(0,cumsum(x[-1L] != x[-length(x)]))+1

tdata %>% ggplot(aes(x=mx)) +  
  geom_line(aes(y=my, color=sign, group=group_transitions(sign)))

这将产生以下结果:

enter image description here