[R] Colouring selected columns in a facetted column chart

phii m@iii@g oii phiiipsmith@c@ phii m@iii@g oii phiiipsmith@c@
Sun Aug 25 04:25:38 CEST 2019


I am having difficulty with a chart using ggplot. It is a facetted 
column chart showing GDP growth rates by country. The columns are 
coloured navyblue, except that I want to colour the most recent columns, 
for 2019-Q1 and 2019-Q2, red. For some countries data are available up 
to 2019-Q2 while for others data are only available up to 2019-Q1. My 
code and data frame are shown below and it almost works, but not quite. 
For some reason the red bars for Germany, Korea, Norway, Sweden and 
United Kingdom are slightly off. Any help will be much appreciated.

Here is my reprex:

library(tidyverse)
t1 <- read.table("t1.txt",header=TRUE,sep="\t")
col <- rep("navyblue",nrow(t1))
for (i in 1:nrow(t1)) {
   if((t1$TIME[i]=="2019-Q1" | t1$TIME[i]=="2019-Q2")) {
     col[i] <- "red"}
}
ggplot(t1) +
   geom_col(aes(x=TIME,y=GDPgr),fill=col) +
   facet_wrap(~Country,ncol=3)

Here is my data frame, called "t1.txt":

"TIME"	"LOCATION"	"Country"	"Value"	"GDPgr"
"2016-Q4"	"AUS"	"Australia"	440518	1
"2017-Q1"	"AUS"	"Australia"	442141	0.4
"2017-Q2"	"AUS"	"Australia"	445739	0.8
"2017-Q3"	"AUS"	"Australia"	448672	0.7
"2017-Q4"	"AUS"	"Australia"	451302	0.6
"2018-Q1"	"AUS"	"Australia"	455680	1
"2018-Q2"	"AUS"	"Australia"	459697	0.9
"2018-Q3"	"AUS"	"Australia"	461024	0.3
"2018-Q4"	"AUS"	"Australia"	462032	0.2
"2019-Q1"	"AUS"	"Australia"	463907	0.4
"2016-Q4"	"BEL"	"Belgium"	106675	0.3
"2017-Q1"	"BEL"	"Belgium"	107394	0.7
"2017-Q2"	"BEL"	"Belgium"	107828	0.4
"2017-Q3"	"BEL"	"Belgium"	108003	0.2
"2017-Q4"	"BEL"	"Belgium"	108744	0.7
"2018-Q1"	"BEL"	"Belgium"	109037	0.3
"2018-Q2"	"BEL"	"Belgium"	109386	0.3
"2018-Q3"	"BEL"	"Belgium"	109676	0.3
"2018-Q4"	"BEL"	"Belgium"	110081	0.4
"2019-Q1"	"BEL"	"Belgium"	110459	0.3
"2019-Q2"	"BEL"	"Belgium"	110680	0.2
"2016-Q4"	"CAN"	"Canada"	493742	0.6
"2017-Q1"	"CAN"	"Canada"	498719	1
"2017-Q2"	"CAN"	"Canada"	504100.5	1.1
"2017-Q3"	"CAN"	"Canada"	505745	0.3
"2017-Q4"	"CAN"	"Canada"	507883	0.4
"2018-Q1"	"CAN"	"Canada"	509758.75	0.4
"2018-Q2"	"CAN"	"Canada"	512958	0.6
"2018-Q3"	"CAN"	"Canada"	515639.25	0.5
"2018-Q4"	"CAN"	"Canada"	515971.75	0.1
"2019-Q1"	"CAN"	"Canada"	516489.5	0.1
"2016-Q4"	"DNK"	"Denmark"	499945	0.9
"2017-Q1"	"DNK"	"Denmark"	511319	2.3
"2017-Q2"	"DNK"	"Denmark"	505254	-1.2
"2017-Q3"	"DNK"	"Denmark"	500363	-1
"2017-Q4"	"DNK"	"Denmark"	504837	0.9
"2018-Q1"	"DNK"	"Denmark"	508633	0.8
"2018-Q2"	"DNK"	"Denmark"	511901	0.6
"2018-Q3"	"DNK"	"Denmark"	513630	0.3
"2018-Q4"	"DNK"	"Denmark"	517726	0.8
"2019-Q1"	"DNK"	"Denmark"	518368	0.1
"2016-Q4"	"EU28"	"European Union (28 countries)"	3301202.652555	0.8
"2017-Q1"	"EU28"	"European Union (28 countries)"	3323886.876398	0.7
"2017-Q2"	"EU28"	"European Union (28 countries)"	3345038.332666	0.6
"2017-Q3"	"EU28"	"European Union (28 countries)"	3367136.027609	0.7
"2017-Q4"	"EU28"	"European Union (28 countries)"	3390431.080785	0.7
"2018-Q1"	"EU28"	"European Union (28 countries)"	3404554.778774	0.4
"2018-Q2"	"EU28"	"European Union (28 countries)"	3419358.570571	0.4
"2018-Q3"	"EU28"	"European Union (28 countries)"	3430321.169276	0.3
"2018-Q4"	"EU28"	"European Union (28 countries)"	3440915.89772	0.3
"2019-Q1"	"EU28"	"European Union (28 countries)"	3458087.265837	0.5
"2019-Q2"	"EU28"	"European Union (28 countries)"	3465003.441	0.2
"2016-Q4"	"FIN"	"Finland"	48525	0.2
"2017-Q1"	"FIN"	"Finland"	49368	1.7
"2017-Q2"	"FIN"	"Finland"	49430	0.1
"2017-Q3"	"FIN"	"Finland"	49596	0.3
"2017-Q4"	"FIN"	"Finland"	50153	1.1
"2018-Q1"	"FIN"	"Finland"	50352	0.4
"2018-Q2"	"FIN"	"Finland"	50449	0.2
"2018-Q3"	"FIN"	"Finland"	50507	0.1
"2018-Q4"	"FIN"	"Finland"	50530	0
"2019-Q1"	"FIN"	"Finland"	50822	0.6
"2016-Q4"	"FRA"	"France"	551760	0.6
"2017-Q1"	"FRA"	"France"	556305	0.8
"2017-Q2"	"FRA"	"France"	560160	0.7
"2017-Q3"	"FRA"	"France"	563998	0.7
"2017-Q4"	"FRA"	"France"	568125	0.7
"2018-Q1"	"FRA"	"France"	569542	0.2
"2018-Q2"	"FRA"	"France"	570670	0.2
"2018-Q3"	"FRA"	"France"	572387	0.3
"2018-Q4"	"FRA"	"France"	574640	0.4
"2019-Q1"	"FRA"	"France"	576494	0.3
"2019-Q2"	"FRA"	"France"	577905	0.2
"2016-Q4"	"DEU"	"Germany"	716743.4074	0.4
"2017-Q1"	"DEU"	"Germany"	725268.5864	1.2
"2017-Q2"	"DEU"	"Germany"	729321.5731	0.6
"2017-Q3"	"DEU"	"Germany"	735610.6375	0.9
"2017-Q4"	"DEU"	"Germany"	740991.229	0.7
"2018-Q1"	"DEU"	"Germany"	741969.5787	0.1
"2018-Q2"	"DEU"	"Germany"	744834.6127	0.4
"2018-Q3"	"DEU"	"Germany"	744065.912	-0.1
"2018-Q4"	"DEU"	"Germany"	745603.2305	0.2
"2019-Q1"	"DEU"	"Germany"	748468.2276	0.4
"2019-Q2"	"DEU"	"Germany"	747909.2496	-0.1
"2016-Q4"	"ISR"	"Israel"	307789.55	0.9
"2017-Q1"	"ISR"	"Israel"	308323.023	0.2
"2017-Q2"	"ISR"	"Israel"	311759.624	1.1
"2017-Q3"	"ISR"	"Israel"	315651.46	1.2
"2017-Q4"	"ISR"	"Israel"	319056.442	1.1
"2018-Q1"	"ISR"	"Israel"	322272.592	1
"2018-Q2"	"ISR"	"Israel"	323422.356	0.4
"2018-Q3"	"ISR"	"Israel"	325702.534	0.7
"2018-Q4"	"ISR"	"Israel"	329052.641	1
"2019-Q1"	"ISR"	"Israel"	332851.725	1.2
"2019-Q2"	"ISR"	"Israel"	333686.876	0.3
"2016-Q4"	"ITA"	"Italy"	396162.2	0.5
"2017-Q1"	"ITA"	"Italy"	398379	0.6
"2017-Q2"	"ITA"	"Italy"	399893	0.4
"2017-Q3"	"ITA"	"Italy"	401534	0.4
"2017-Q4"	"ITA"	"Italy"	403053.4	0.4
"2018-Q1"	"ITA"	"Italy"	403937.8	0.2
"2018-Q2"	"ITA"	"Italy"	403977.3	0
"2018-Q3"	"ITA"	"Italy"	403434.2	-0.1
"2018-Q4"	"ITA"	"Italy"	403190.7	-0.1
"2019-Q1"	"ITA"	"Italy"	403697.9	0.1
"2019-Q2"	"ITA"	"Italy"	403794.7	0
"2016-Q4"	"JPN"	"Japan"	130406025	0.2
"2017-Q1"	"JPN"	"Japan"	131558850	0.9
"2017-Q2"	"JPN"	"Japan"	132121450	0.4
"2017-Q3"	"JPN"	"Japan"	133064400	0.7
"2017-Q4"	"JPN"	"Japan"	133475100	0.3
"2018-Q1"	"JPN"	"Japan"	133386850	-0.1
"2018-Q2"	"JPN"	"Japan"	133931825	0.4
"2018-Q3"	"JPN"	"Japan"	133289800	-0.5
"2018-Q4"	"JPN"	"Japan"	133836225	0.4
"2019-Q1"	"JPN"	"Japan"	134777725	0.7
"2019-Q2"	"JPN"	"Japan"	135369050	0.4
"2016-Q4"	"KOR"	"Korea"	431473400	0.8
"2017-Q1"	"KOR"	"Korea"	435435200	0.9
"2017-Q2"	"KOR"	"Korea"	437712100	0.5
"2017-Q3"	"KOR"	"Korea"	444064400	1.5
"2017-Q4"	"KOR"	"Korea"	443599800	-0.1
"2018-Q1"	"KOR"	"Korea"	447909300	1
"2018-Q2"	"KOR"	"Korea"	450495800	0.6
"2018-Q3"	"KOR"	"Korea"	452561100	0.5
"2018-Q4"	"KOR"	"Korea"	456769700	0.9
"2019-Q1"	"KOR"	"Korea"	455081000	-0.4
"2019-Q2"	"KOR"	"Korea"	459958000	1.1
"2016-Q4"	"NLD"	"Netherlands"	178453.593134	0.9
"2017-Q1"	"NLD"	"Netherlands"	179367.793134	0.5
"2017-Q2"	"NLD"	"Netherlands"	180964.533134	0.9
"2017-Q3"	"NLD"	"Netherlands"	182189.893134	0.7
"2017-Q4"	"NLD"	"Netherlands"	183625.193134	0.8
"2018-Q1"	"NLD"	"Netherlands"	184793.473134	0.6
"2018-Q2"	"NLD"	"Netherlands"	185981.973134	0.6
"2018-Q3"	"NLD"	"Netherlands"	186425.153134	0.2
"2018-Q4"	"NLD"	"Netherlands"	187434.343134	0.5
"2019-Q1"	"NLD"	"Netherlands"	188324.263134	0.5
"2019-Q2"	"NLD"	"Netherlands"	189297.773134	0.5
"2016-Q4"	"NZL"	"New Zealand"	59062	0.5
"2017-Q1"	"NZL"	"New Zealand"	59348	0.5
"2017-Q2"	"NZL"	"New Zealand"	59743	0.7
"2017-Q3"	"NZL"	"New Zealand"	60320	1
"2017-Q4"	"NZL"	"New Zealand"	60737	0.7
"2018-Q1"	"NZL"	"New Zealand"	61031	0.5
"2018-Q2"	"NZL"	"New Zealand"	61655	1
"2018-Q3"	"NZL"	"New Zealand"	61927	0.4
"2018-Q4"	"NZL"	"New Zealand"	62282	0.6
"2019-Q1"	"NZL"	"New Zealand"	62800	0.8
"2016-Q4"	"NOR"	"Norway"	784704	2
"2017-Q1"	"NOR"	"Norway"	788709	0.5
"2017-Q2"	"NOR"	"Norway"	794220	0.7
"2017-Q3"	"NOR"	"Norway"	798283	0.5
"2017-Q4"	"NOR"	"Norway"	800232	0.2
"2018-Q1"	"NOR"	"Norway"	803756	0.4
"2018-Q2"	"NOR"	"Norway"	807187	0.4
"2018-Q3"	"NOR"	"Norway"	810942	0.5
"2018-Q4"	"NOR"	"Norway"	815921	0.6
"2019-Q1"	"NOR"	"Norway"	815323	-0.1
"2016-Q4"	"PRT"	"Portugal"	44303.821	0.8
"2017-Q1"	"PRT"	"Portugal"	44632.068	0.7
"2017-Q2"	"PRT"	"Portugal"	44803.721	0.4
"2017-Q3"	"PRT"	"Portugal"	45062.631	0.6
"2017-Q4"	"PRT"	"Portugal"	45426.14	0.8
"2018-Q1"	"PRT"	"Portugal"	45641.37	0.5
"2018-Q2"	"PRT"	"Portugal"	45910.934	0.6
"2018-Q3"	"PRT"	"Portugal"	46027.362	0.3
"2018-Q4"	"PRT"	"Portugal"	46203.578	0.4
"2019-Q1"	"PRT"	"Portugal"	46453.392	0.5
"2019-Q2"	"PRT"	"Portugal"	46685.65896	0.5
"2016-Q4"	"ESP"	"Spain"	279431	0.6
"2017-Q1"	"ESP"	"Spain"	281707	0.8
"2017-Q2"	"ESP"	"Spain"	284169	0.9
"2017-Q3"	"ESP"	"Spain"	285986	0.6
"2017-Q4"	"ESP"	"Spain"	288064	0.7
"2018-Q1"	"ESP"	"Spain"	289861	0.6
"2018-Q2"	"ESP"	"Spain"	291583	0.6
"2018-Q3"	"ESP"	"Spain"	293145	0.5
"2018-Q4"	"ESP"	"Spain"	294768	0.6
"2019-Q1"	"ESP"	"Spain"	296732	0.7
"2019-Q2"	"ESP"	"Spain"	298147	0.5
"2016-Q4"	"SWE"	"Sweden"	1150761	0.4
"2017-Q1"	"SWE"	"Sweden"	1151977	0.1
"2017-Q2"	"SWE"	"Sweden"	1169243	1.5
"2017-Q3"	"SWE"	"Sweden"	1177835	0.7
"2017-Q4"	"SWE"	"Sweden"	1181734	0.3
"2018-Q1"	"SWE"	"Sweden"	1192111	0.9
"2018-Q2"	"SWE"	"Sweden"	1197931	0.5
"2018-Q3"	"SWE"	"Sweden"	1196262	-0.1
"2018-Q4"	"SWE"	"Sweden"	1209430	1.1
"2019-Q1"	"SWE"	"Sweden"	1215583	0.5
"2019-Q2"	"SWE"	"Sweden"	1214691	-0.1
"2016-Q4"	"CHE"	"Switzerland"	168268.356822	-0.1
"2017-Q1"	"CHE"	"Switzerland"	168865.076317	0.4
"2017-Q2"	"CHE"	"Switzerland"	170078.764694	0.7
"2017-Q3"	"CHE"	"Switzerland"	171405.16327	0.8
"2017-Q4"	"CHE"	"Switzerland"	172777.427869	0.8
"2018-Q1"	"CHE"	"Switzerland"	174168.535837	0.8
"2018-Q2"	"CHE"	"Switzerland"	175400.870886	0.7
"2018-Q3"	"CHE"	"Switzerland"	175089.0314	-0.2
"2018-Q4"	"CHE"	"Switzerland"	175664.228343	0.3
"2019-Q1"	"CHE"	"Switzerland"	176651.744992	0.6
"2016-Q4"	"GBR"	"United Kingdom"	496470	0.7
"2017-Q1"	"GBR"	"United Kingdom"	498582	0.4
"2017-Q2"	"GBR"	"United Kingdom"	499885	0.3
"2017-Q3"	"GBR"	"United Kingdom"	502473	0.5
"2017-Q4"	"GBR"	"United Kingdom"	504487	0.4
"2018-Q1"	"GBR"	"United Kingdom"	504785	0.1
"2018-Q2"	"GBR"	"United Kingdom"	506842	0.4
"2018-Q3"	"GBR"	"United Kingdom"	510346	0.7
"2018-Q4"	"GBR"	"United Kingdom"	511482	0.2
"2019-Q1"	"GBR"	"United Kingdom"	514019	0.5
"2019-Q2"	"GBR"	"United Kingdom"	513029	-0.2
"2016-Q4"	"USA"	"United States"	4456057.75	0.5
"2017-Q1"	"USA"	"United States"	4481314	0.6
"2017-Q2"	"USA"	"United States"	4505262	0.5
"2017-Q3"	"USA"	"United States"	4540889.5	0.8
"2017-Q4"	"USA"	"United States"	4580616	0.9
"2018-Q1"	"USA"	"United States"	4609563.5	0.6
"2018-Q2"	"USA"	"United States"	4649533.75	0.9
"2018-Q3"	"USA"	"United States"	4683180	0.7
"2018-Q4"	"USA"	"United States"	4695887	0.3
"2019-Q1"	"USA"	"United States"	4731820.25	0.8
"2019-Q2"	"USA"	"United States"	4755955	0.5



More information about the R-help mailing list