Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I color positive and negative values differently in plot

Tags:

r

ggplot2

I have the following data:

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

My second dataset:

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

I now plot both of these graphs together:

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

I get the following beautiful plot. But to convey more info, what I would like to have is the negative values from df2 data to be shown in say red (values below 0 line) and positive values in say green.I did create a logical column in df2 having TRUE/FALSE values but I am unable to get the different colors.

like image 230
Nishant Avatar asked Mar 12 '23 05:03

Nishant


1 Answers

Try just using 'color=value< 0'. Granted this is in qplot, but it should function the same with the geom functions.

> 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

like image 185
ManlikeChive Avatar answered Apr 30 '23 23:04

ManlikeChive