<The way to color bar graphs>
1. geom_bar(colour=, fill=)
colour= color the border of graph
fill= fill the graph
> ggplot(data=mpg,aes(x=cyl))+geom_bar()
> ggplot(data=mpg,aes(x=cyl))+geom_bar(fill="blue")
> ggplot(data=mpg,aes(x=cyl))+geom_bar(fill="white",colour="blue")
2. fill graphs separately by a variable.
-geom_bar(x=, y=, fill= 'a variable')
-geom_bar()+scale_fill_brewer
-geom_bar()+scale_fill_manual
>library(ggplot2)
>library(dplyr)
> summary(airquality$Ozone)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
1.00 18.00 31.50 42.13 63.25 168.00 37
> summary(airquality$Temp)
Min. 1st Qu. Median Mean 3rd Qu. Max.
56.00 72.00 79.00 77.88 85.00 97.00
> airquality_<-airquality%>%filter(!is.na(Ozone))%>%mutate(Ozone_rank=ifelse(Ozone<=31.50,"little",ifelse(Ozone<=63.25,"middle","much")))%>%mutate(temp_rank=ifelse(Temp<=72,"warm",ifelse(Temp<=85,"hot","very hot")))
> table(airquality_$Ozone_rank)
little middle much
58 29 29
> table(airquality_$temp_rank)
hot very hot warm
56 27 33
> Ozone_temp<-airquality_%>%group_by(Ozone_rank)%>%summarise(mean_temp=mean(Temp))
> Ozone_temp_rate<-airquality_%>%count(Ozone_rank,temp_rank)%>%group_by(Ozone_rank)%>%mutate(per=n/sum(n)*100)
> ggplot(data=Ozone_temp_rate,aes(x=Ozone_rank,y=per,fill=temp_rank))+geom_bar(stat="identity")
> ggplot(data=Ozone_temp_rate,aes(x=Ozone_rank,y=per,fill=temp_rank))+geom_bar(position="dodge",stat="identity")
> ggplot(data=Ozone_temp_rate,aes(x=Ozone_rank,y=per,fill=temp_rank))+geom_bar(position="dodge",colour="black",stat="identity")
> ggplot(data=Ozone_temp_rate,aes(x=Ozone_rank,y=per,fill=temp_rank))+geom_bar(position="dodge",colour="black",stat="identity")+scale_fill_brewer(palette=1)
> ggplot(data=Ozone_temp_rate,aes(x=Ozone_rank,y=per,fill=temp_rank))+geom_bar(position="dodge",colour="black",stat="identity")+scale_fill_brewer(palette="Pastel1")
> ggplot(data=Ozone_temp_rate,aes(x=reorder(Ozone_rank,per),y=per,fill=temp_rank))+geom_bar(position="dodge",colour="black",stat="identity")+scale_fill_brewer(palette = 2)+xlab("Ozone")
xlab() is the function needed for designating x-axis as a specific name "Ozone"
> ggplot(data=Ozone_temp_rate,aes(x=reorder(Ozone_rank,per),y=per,fill=temp_rank))+geom_bar(position="dodge",colour="black",stat="identity")+scale_fill_manual(values=c("blue1","lightblue","blueviolet"))+xlab("Ozone")
<colour diversity in ggplot2>
>?colour
> grDevices::colors()
[1] "white" "aliceblue"
[3] "antiquewhite" "antiquewhite1"
[5] "antiquewhite2" "antiquewhite3"
[7] "antiquewhite4" "aquamarine"
[9] "aquamarine1" "aquamarine2"
[11] "aquamarine3" "aquamarine4"
[13] "azure" "azure1"
[15] "azure2" "azure3"
[17] "azure4" "beige"
[19] "bisque" "bisque1"
[21] "bisque2" "bisque3"
[23] "bisque4" "black"
[25] "blanchedalmond" "blue"
[27] "blue1" "blue2"
[29] "blue3" "blue4"
[31] "blueviolet" "brown"
[33] "brown1" "brown2"
[35] "brown3" "brown4"
[37] "burlywood" "burlywood1"
[39] "burlywood2" "burlywood3"
[41] "burlywood4" "cadetblue"
[43] "cadetblue1" "cadetblue2"
[45] "cadetblue3" "cadetblue4"
[47] "chartreuse" "chartreuse1"
[49] "chartreuse2" "chartreuse3"
[51] "chartreuse4" "chocolate"
[53] "chocolate1" "chocolate2"
[55] "chocolate3" "chocolate4"
[57] "coral" "coral1"
[59] "coral2" "coral3"
[61] "coral4" "cornflowerblue"
[63] "cornsilk" "cornsilk1"
[65] "cornsilk2" "cornsilk3"
[67] "cornsilk4" "cyan"
[69] "cyan1" "cyan2"
[71] "cyan3" "cyan4"
[73] "darkblue" "darkcyan"
[75] "darkgoldenrod" "darkgoldenrod1"
[77] "darkgoldenrod2" "darkgoldenrod3"
[79] "darkgoldenrod4" "darkgray"
[81] "darkgreen" "darkgrey"
[83] "darkkhaki" "darkmagenta"
[85] "darkolivegreen" "darkolivegreen1"
[87] "darkolivegreen2" "darkolivegreen3"
[89] "darkolivegreen4" "darkorange"
[91] "darkorange1" "darkorange2"
[93] "darkorange3" "darkorange4"
[95] "darkorchid" "darkorchid1"
[97] "darkorchid2" "darkorchid3"
[99] "darkorchid4" "darkred"
[101] "darksalmon" "darkseagreen"
[103] "darkseagreen1" "darkseagreen2"
[105] "darkseagreen3" "darkseagreen4"
[107] "darkslateblue" "darkslategray"
[109] "darkslategray1" "darkslategray2"
[111] "darkslategray3" "darkslategray4"
[113] "darkslategrey" "darkturquoise"
[115] "darkviolet" "deeppink"
[117] "deeppink1" "deeppink2"
[119] "deeppink3" "deeppink4"
[121] "deepskyblue" "deepskyblue1"
[123] "deepskyblue2" "deepskyblue3"
[125] "deepskyblue4" "dimgray"
[127] "dimgrey" "dodgerblue"
[129] "dodgerblue1" "dodgerblue2"
[131] "dodgerblue3" "dodgerblue4"
[133] "firebrick" "firebrick1"
[135] "firebrick2" "firebrick3"
[137] "firebrick4" "floralwhite"
[139] "forestgreen" "gainsboro"
[141] "ghostwhite" "gold"
[143] "gold1" "gold2"
[145] "gold3" "gold4"
[147] "goldenrod" "goldenrod1"
[149] "goldenrod2" "goldenrod3"
[151] "goldenrod4" "gray"
[153] "gray0" "gray1"
[155] "gray2" "gray3"
[157] "gray4" "gray5"
[159] "gray6" "gray7"
[161] "gray8" "gray9"
[163] "gray10" "gray11"
[165] "gray12" "gray13"
[167] "gray14" "gray15"
[169] "gray16" "gray17"
[171] "gray18" "gray19"
[173] "gray20" "gray21"
[175] "gray22" "gray23"
[177] "gray24" "gray25"
[179] "gray26" "gray27"
[181] "gray28" "gray29"
[183] "gray30" "gray31"
[185] "gray32" "gray33"
[187] "gray34" "gray35"
[189] "gray36" "gray37"
[191] "gray38" "gray39"
[193] "gray40" "gray41"
[195] "gray42" "gray43"
[197] "gray44" "gray45"
[199] "gray46" "gray47"
[201] "gray48" "gray49"
[203] "gray50" "gray51"
[205] "gray52" "gray53"
[207] "gray54" "gray55"
[209] "gray56" "gray57"
[211] "gray58" "gray59"
[213] "gray60" "gray61"
[215] "gray62" "gray63"
[217] "gray64" "gray65"
[219] "gray66" "gray67"
[221] "gray68" "gray69"
[223] "gray70" "gray71"
[225] "gray72" "gray73"
[227] "gray74" "gray75"
[229] "gray76" "gray77"
[231] "gray78" "gray79"
[233] "gray80" "gray81"
[235] "gray82" "gray83"
[237] "gray84" "gray85"
[239] "gray86" "gray87"
[241] "gray88" "gray89"
[243] "gray90" "gray91"
[245] "gray92" "gray93"
[247] "gray94" "gray95"
[249] "gray96" "gray97"
[251] "gray98" "gray99"
[253] "gray100" "green"
[255] "green1" "green2"
[257] "green3" "green4"
[259] "greenyellow" "grey"
[261] "grey0" "grey1"
[263] "grey2" "grey3"
[265] "grey4" "grey5"
[267] "grey6" "grey7"
[269] "grey8" "grey9"
[271] "grey10" "grey11"
[273] "grey12" "grey13"
[275] "grey14" "grey15"
[277] "grey16" "grey17"
[279] "grey18" "grey19"
[281] "grey20" "grey21"
[283] "grey22" "grey23"
[285] "grey24" "grey25"
[287] "grey26" "grey27"
[289] "grey28" "grey29"
[291] "grey30" "grey31"
[293] "grey32" "grey33"
[295] "grey34" "grey35"
[297] "grey36" "grey37"
[299] "grey38" "grey39"
[301] "grey40" "grey41"
[303] "grey42" "grey43"
[305] "grey44" "grey45"
[307] "grey46" "grey47"
[309] "grey48" "grey49"
[311] "grey50" "grey51"
[313] "grey52" "grey53"
[315] "grey54" "grey55"
[317] "grey56" "grey57"
[319] "grey58" "grey59"
[321] "grey60" "grey61"
[323] "grey62" "grey63"
[325] "grey64" "grey65"
[327] "grey66" "grey67"
[329] "grey68" "grey69"
[331] "grey70" "grey71"
[333] "grey72" "grey73"
[335] "grey74" "grey75"
[337] "grey76" "grey77"
[339] "grey78" "grey79"
[341] "grey80" "grey81"
[343] "grey82" "grey83"
[345] "grey84" "grey85"
[347] "grey86" "grey87"
[349] "grey88" "grey89"
[351] "grey90" "grey91"
[353] "grey92" "grey93"
[355] "grey94" "grey95"
[357] "grey96" "grey97"
[359] "grey98" "grey99"
[361] "grey100" "honeydew"
[363] "honeydew1" "honeydew2"
[365] "honeydew3" "honeydew4"
[367] "hotpink" "hotpink1"
[369] "hotpink2" "hotpink3"
[371] "hotpink4" "indianred"
[373] "indianred1" "indianred2"
[375] "indianred3" "indianred4"
[377] "ivory" "ivory1"
[379] "ivory2" "ivory3"
[381] "ivory4" "khaki"
[383] "khaki1" "khaki2"
[385] "khaki3" "khaki4"
[387] "lavender" "lavenderblush"
[389] "lavenderblush1" "lavenderblush2"
[391] "lavenderblush3" "lavenderblush4"
[393] "lawngreen" "lemonchiffon"
[395] "lemonchiffon1" "lemonchiffon2"
[397] "lemonchiffon3" "lemonchiffon4"
[399] "lightblue" "lightblue1"
[401] "lightblue2" "lightblue3"
[403] "lightblue4" "lightcoral"
[405] "lightcyan" "lightcyan1"
[407] "lightcyan2" "lightcyan3"
[409] "lightcyan4" "lightgoldenrod"
[411] "lightgoldenrod1" "lightgoldenrod2"
[413] "lightgoldenrod3" "lightgoldenrod4"
[415] "lightgoldenrodyellow" "lightgray"
[417] "lightgreen" "lightgrey"
[419] "lightpink" "lightpink1"
[421] "lightpink2" "lightpink3"
[423] "lightpink4" "lightsalmon"
[425] "lightsalmon1" "lightsalmon2"
[427] "lightsalmon3" "lightsalmon4"
[429] "lightseagreen" "lightskyblue"
[431] "lightskyblue1" "lightskyblue2"
[433] "lightskyblue3" "lightskyblue4"
[435] "lightslateblue" "lightslategray"
[437] "lightslategrey" "lightsteelblue"
[439] "lightsteelblue1" "lightsteelblue2"
[441] "lightsteelblue3" "lightsteelblue4"
[443] "lightyellow" "lightyellow1"
[445] "lightyellow2" "lightyellow3"
[447] "lightyellow4" "limegreen"
[449] "linen" "magenta"
[451] "magenta1" "magenta2"
[453] "magenta3" "magenta4"
[455] "maroon" "maroon1"
[457] "maroon2" "maroon3"
[459] "maroon4" "mediumaquamarine"
[461] "mediumblue" "mediumorchid"
[463] "mediumorchid1" "mediumorchid2"
[465] "mediumorchid3" "mediumorchid4"
[467] "mediumpurple" "mediumpurple1"
[469] "mediumpurple2" "mediumpurple3"
[471] "mediumpurple4" "mediumseagreen"
[473] "mediumslateblue" "mediumspringgreen"
[475] "mediumturquoise" "mediumvioletred"
[477] "midnightblue" "mintcream"
[479] "mistyrose" "mistyrose1"
[481] "mistyrose2" "mistyrose3"
[483] "mistyrose4" "moccasin"
[485] "navajowhite" "navajowhite1"
[487] "navajowhite2" "navajowhite3"
[489] "navajowhite4" "navy"
[491] "navyblue" "oldlace"
[493] "olivedrab" "olivedrab1"
[495] "olivedrab2" "olivedrab3"
[497] "olivedrab4" "orange"
[499] "orange1" "orange2"
[501] "orange3" "orange4"
[503] "orangered" "orangered1"
[505] "orangered2" "orangered3"
[507] "orangered4" "orchid"
[509] "orchid1" "orchid2"
[511] "orchid3" "orchid4"
[513] "palegoldenrod" "palegreen"
[515] "palegreen1" "palegreen2"
[517] "palegreen3" "palegreen4"
[519] "paleturquoise" "paleturquoise1"
[521] "paleturquoise2" "paleturquoise3"
[523] "paleturquoise4" "palevioletred"
[525] "palevioletred1" "palevioletred2"
[527] "palevioletred3" "palevioletred4"
[529] "papayawhip" "peachpuff"
[531] "peachpuff1" "peachpuff2"
[533] "peachpuff3" "peachpuff4"
[535] "peru" "pink"
[537] "pink1" "pink2"
[539] "pink3" "pink4"
[541] "plum" "plum1"
[543] "plum2" "plum3"
[545] "plum4" "powderblue"
[547] "purple" "purple1"
[549] "purple2" "purple3"
[551] "purple4" "red"
[553] "red1" "red2"
[555] "red3" "red4"
[557] "rosybrown" "rosybrown1"
[559] "rosybrown2" "rosybrown3"
[561] "rosybrown4" "royalblue"
[563] "royalblue1" "royalblue2"
[565] "royalblue3" "royalblue4"
[567] "saddlebrown" "salmon"
[569] "salmon1" "salmon2"
[571] "salmon3" "salmon4"
[573] "sandybrown" "seagreen"
[575] "seagreen1" "seagreen2"
[577] "seagreen3" "seagreen4"
[579] "seashell" "seashell1"
[581] "seashell2" "seashell3"
[583] "seashell4" "sienna"
[585] "sienna1" "sienna2"
[587] "sienna3" "sienna4"
[589] "skyblue" "skyblue1"
[591] "skyblue2" "skyblue3"
[593] "skyblue4" "slateblue"
[595] "slateblue1" "slateblue2"
[597] "slateblue3" "slateblue4"
[599] "slategray" "slategray1"
[601] "slategray2" "slategray3"
[603] "slategray4" "slategrey"
[605] "snow" "snow1"
[607] "snow2" "snow3"
[609] "snow4" "springgreen"
[611] "springgreen1" "springgreen2"
[613] "springgreen3" "springgreen4"
[615] "steelblue" "steelblue1"
[617] "steelblue2" "steelblue3"
[619] "steelblue4" "tan"
[621] "tan1" "tan2"
[623] "tan3" "tan4"
[625] "thistle" "thistle1"
[627] "thistle2" "thistle3"
[629] "thistle4" "tomato"
[631] "tomato1" "tomato2"
[633] "tomato3" "tomato4"
[635] "turquoise" "turquoise1"
[637] "turquoise2" "turquoise3"
[639] "turquoise4" "violet"
[641] "violetred" "violetred1"
[643] "violetred2" "violetred3"
[645] "violetred4" "wheat"
[647] "wheat1" "wheat2"
[649] "wheat3" "wheat4"
[651] "whitesmoke" "yellow"
[653] "yellow1" "yellow2"
[655] "yellow3" "yellow4"
[657] "yellowgreen"