Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expand colour palette in ggplot2

Tags:

r

ggplot2

Hi everybody I am making a graphic in ggplot2 but I wish to add different colour to it. One way is to use palette from RColorBrewer package. I used a palette from this package but I got this. The code that I used is the next:

library(ggplot2)
library(reshape2)
library(scales)
library(RColorBrewer)
#Graphic
png(filename = "Atotal.png", width = 1050, height = 900)
Atotal=ggplot(Melt.Atotal, aes(x = Var2, y = value, fill = Mes)) +
  geom_bar(stat = "identity") + scale_y_continuous(labels = comma) + scale_fill_brewer(palette="Set3") +
  annotate("text", x = ncol(AtotalM) - 1.3, y = finalValA1 + 4000000, 
           label = finalValA1f, size = 5) + theme(axis.text.x=element_text(angle=90,colour="grey20",face="bold"),axis.text.y=element_text(colour="grey20",face="bold",hjust=1,vjust=0.8),axis.title.x=element_text(colour="grey20",face="bold"),axis.title.y=element_text(colour="grey20",face="bold"))+xlab('Mes')+ylab('Total')+ ggtitle("Consumo Automotriz (A)")+theme(plot.title = element_text(lineheight=3, face="bold", color="black",family="F",size=24))
print(Atotal)
dev.off()

The result of using scale_fill_brewer(palette="Set3") inside this code is the next:

enter image description here

Where not all values of Mes are being considered. My data is Melt.Atotal and it has 3 variables Mes, Var2 and Value. The code to build Melt.Atotal is the next (I add dput() version of my basic matrix):

Melt.Atotal <- melt(AtotalMt)
Melt.Atotal<-transform(Melt.Atotal, Var1 = factor(Var1, colnames(AtotalMt)),
                       Var2 = factor(Var2, rownames(AtotalMt)))
names(Melt.Atotal)[1] <- "Mes"

The dput() version of AtotalMt is the next:

structure(c(26854799.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 25308105.96, 2048291.06, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23675677.38, 2019295.58, 3043637.6, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22325824.08, 
1977947.76, 2994958.95, 3669634.09, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 20947118.18, 1945806.86, 2870951.11, 3623960.37, 
3641665.16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20106655.89, 
1919815.43, 2827562.38, 3557471.33, 3598447.3, 6780879.37, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18667088.88, 1776866.25, 
2728334.76, 3481461.59, 3537170.97, 6703991.88, 6229245.09, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16845310.67, 1656723.88, 2649675.68, 
3331967.07, 3393759.08, 6514307.07, 6101157.64, 5553163.01, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15930902.54, 1643219.04, 2511944.81, 
3284255.37, 3243600.72, 6392642.61, 5931591.11, 5456712.16, 6372919.9, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14805849.82, 1615497.99, 2485435.88, 
3160371.22, 3220851.12, 6205541.07, 5782747.54, 5289908.54, 6228697.79, 
6755054.64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13598279.47, 1543252.95, 
2406932.77, 3079437.34, 3066205.85, 5939178.65, 5418784.54, 5146445.86, 
6057527.35, 6564332.71, 7119008.27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12795858.11, 
1513931.53, 2364512.31, 2968182.7, 2974803.21, 5750644.13, 5209696.29, 
4974437.54, 5824911.96, 6496834.58, 6982259.62, 7585411.87, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 11758983.37, 1463851.51, 2257773.94, 2908980.44, 
2842786.2, 5571236.68, 5231547.64, 4843015.16, 5645654.72, 6291395.12, 
6849018.72, 7394553.35, 7258675.63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10777426.87, 
1444585.43, 2205824.51, 2856067.03, 2656671.24, 5387412.76, 4891235.85, 
4788887.82, 5403486.34, 6021463.61, 6669060.68, 7121591.46, 7112320.54, 
7250944.21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 10266106.58, 1451698.54, 2040868.4, 
2834443.74, 2607311.71, 5350935.23, 4986150.11, 4700554.49, 5283671.93, 
5995160.44, 6464991.3, 7090338.26, 6962156.01, 7163824.09, 6211543.17, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 9282265.15, 1371250.56, 2017270.91, 2722300.36, 2471954.64, 
5130736.05, 4713668.34, 4465135.85, 5168537.36, 5754071.76, 6297559.65, 
6915271.02, 6772197.84, 6959577.9, 6118091.32, 5795989.01, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 8464668.83, 1283914.03, 1845320.7, 2660890.82, 2377866.52, 
4857393.18, 4469951.91, 4290172.82, 4970032.4, 5530336.24, 6077911.67, 
6606098.28, 6476328.51, 6705017.06, 5935609.39, 5640705.86, 4015226.43, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 7918789.87, 1279609.75, 1881248.3, 2587873.16, 2364394.91, 
4906921.98, 4445721.03, 4284628.74, 4946522.49, 5554209.39, 6014383.31, 
6680961.12, 6344399.8, 6655965.34, 5709641.38, 5550978.64, 3964045.05, 
5391952.87, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 7198508.1, 1208751.41, 1724855.79, 2499547.5, 
2256276.46, 4558960.92, 4221626.91, 4091117.89, 4770936.83, 5236843.82, 
5714312.29, 6328556.64, 6050113.53, 6477665.43, 5525707.17, 5373406.76, 
3873662.43, 5267242.72, 4367910.18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6463505.48, 1118475.47, 1686515.09, 
2358748.13, 2101583, 4368585.08, 4032671.17, 3754992.4, 4554872.52, 
5088464.45, 5416254.09, 5949658.57, 5820410.69, 6123152.54, 5241203.61, 
5117928.97, 3780923.84, 5064923.02, 4319577.61, 4248144.11, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5981522.92, 
1113893.16, 1649654.18, 2258165.09, 1984585.89, 4182216.8, 3876676.8, 
3736577.48, 4361915.64, 4967747.45, 5421522.41, 5744184.46, 5739301.38, 
5990201.18, 5096379.5, 5013509.94, 3661788.44, 4985665.85, 4215411.02, 
4162236.22, 3537103.4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 5553863.8, 1042559.75, 1566422.05, 2205045.56, 
1924796.63, 4080172.4, 3725283.16, 3542074.94, 4244071.75, 4612471.39, 
5182532.78, 5709173.1, 5525638.03, 5944624.74, 4901061.55, 4891438.46, 
3509531.92, 4841766.87, 4178459.26, 4010745.77, 3481936.55, 3500595.75, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4918517.28, 
964413.4, 1487320.83, 2052062.38, 1854727.08, 3863049.56, 3538505.25, 
3325688.5, 3960168.53, 4422232.88, 4840572.76, 5305625.33, 5228785.95, 
5595390.47, 4697854.89, 4534514.66, 3216124.85, 4666951.83, 3871215.6, 
3831335.12, 3319421.71, 3426985.34, 4049074.18, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4589865.7, 954115.22, 1383875.73, 
2006750.82, 1704060.98, 3710278.82, 3440856.49, 3284324.63, 3881579.99, 
4279966.57, 4876374.02, 5216127.78, 5240486.97, 5463726.92, 4599517.2, 
4565728.74, 3369335.09, 4546853.72, 3913112.94, 3717953.98, 3278679.59, 
3373973.71, 4013406.3, 3279868.96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 4176804.11, 888990.38, 1333185.64, 1934096.36, 
1675497.76, 3557827.91, 3299389.42, 3079591.31, 3702946.61, 3992278.82, 
4523001.43, 4981117.99, 4975445.91, 5377398.51, 4533561.81, 4382021.43, 
3213712.98, 4381408.86, 3781808.1, 3625319.37, 3204382.69, 3245757.92, 
3951884.98, 3231005.5, 3420974.23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 3883820.29, 813949.38, 1265350.22, 1841176.88, 
1603582.53, 3405314.93, 3141362.72, 2807968.09, 3492954.57, 3697604.71, 
4308660.02, 4666512.67, 4709968.33, 5017240.81, 4268580.97, 4145817.08, 
3067212.11, 4151094.71, 3655952.66, 3468151.14, 3133063.94, 3107297.63, 
3809850.33, 3162485.62, 3372197.02, 2602310.3, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 3644460.37, 781745.08, 1204720.53, 
1800978.26, 1554501.31, 3201603.18, 2959079.05, 2739545.56, 3374793.64, 
3525695.81, 4291334.59, 4638114.93, 4666374.2, 4892572.95, 4116606.75, 
4112691.46, 3043441.02, 4125176.99, 3623072, 3349178.97, 3084061.59, 
3084271.42, 3774049.41, 3106863.28, 3295038.38, 2544260.71, 2225635.25, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2725532.3, 695385.39, 
1089829.11, 1635866.34, 1441894.1, 2946913.04, 2844256.31, 2690137.46, 
3035217.37, 3334633.77, 4005528.51, 4022237.61, 4214648.75, 4672725.89, 
3983322.49, 3919345.67, 2756902.02, 3934296.19, 3393367.29, 3235404.74, 
2889134.26, 3024263.51, 3568656.75, 2920654.06, 3178506.86, 2507768.27, 
2186791.52, 2853467.41, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3049966.84, 
683350.29, 1074527.94, 1664090.14, 1345253.75, 2839637.6, 2651098.47, 
2500948.78, 3040646.89, 3272387, 3923685.61, 4224081.82, 4210472.99, 
4582848.41, 3755426.17, 3782606.32, 2795678.59, 3898307.39, 3434053.44, 
3121821.28, 2921363.22, 2877461.21, 3512748.84, 2945195.1, 3094739.51, 
2418918.61, 2056727.84, 2784556.5, 2830482.27, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 2798410.64, 639895.71, 1004919.36, 1510655.38, 
1304308.81, 2740725.42, 2558504.6, 2475013.76, 2939349.12, 3178696.36, 
3747969.66, 4000867.51, 4125018.83, 4349776.89, 3632633.64, 3675764.49, 
2687950.7, 3774205.99, 3431224, 3079305.21, 2816046.7, 2859152.69, 
3393940.45, 2877006.74, 3076307.65, 2326266.7, 2057970.61, 2736991.2, 
2776566.7, 2823590.65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2399320.7, 
624816.39, 965440.49, 1456986.54, 1215055.86, 2553035.48, 2435169.62, 
2332822.03, 2787520.88, 2956835.71, 3408436.53, 3793385.58, 3725354.01, 
4083495.32, 3402962.8, 3315638.6, 2431920.15, 3438929.71, 3132830.19, 
2904787.73, 2662365.19, 2753083.53, 3258576.92, 2806899.51, 2885600.76, 
2215268.28, 1950059.9, 2607609.51, 2699206.83, 2765967.81, 3006568.05, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 2201458.94, 571519.44, 843925.91, 
1371876.38, 1178678.1, 2443914.2, 2318665.11, 2252903.7, 2617795.14, 
2859138.88, 3304349.3, 3686591.89, 3674731.78, 3966378.3, 3378997.7, 
3214415.84, 2405071.88, 3366308.49, 2990342.49, 2736475.12, 2607168.86, 
2706348.96, 3213557.76, 2599903.38, 2824306.46, 2164763.75, 1937899.61, 
2579777.7, 2626362.72, 2721182.87, 2931342.61, 2615730, 0, 0, 
0, 0, 0, 0, 0, 0, 1959992.56, 509529.58, 799675.48, 1242389.68, 
1141241.83, 2260667.56, 2179604.89, 2159947.31, 2489487.53, 2626465.58, 
3155508.33, 3557859.64, 3464663.89, 3773518.5, 3198567.51, 3252935.25, 
2278042.82, 3273402.14, 2865815.76, 2602186.5, 2577796.07, 2571460.82, 
3175856.78, 2578702.16, 2776048.48, 2096759.57, 1877479.59, 2468371.84, 
2524149.35, 2661768.7, 2882642.88, 2569279.36, 2833299.1, 0, 
0, 0, 0, 0, 0, 0, 1729244.67, 462014.66, 727394.64, 1180017.89, 
1095320.94, 2183552.82, 2109743.44, 2086989.93, 2345316.13, 2626975.96, 
3068532.68, 3411689.2, 3319895.42, 3722715.8, 3172978.81, 3109894.93, 
2209191.15, 3158834.79, 2838699.9, 2480821.5, 2493415.44, 2528070.2, 
3080337.25, 2481988.35, 2707671.27, 2046364.76, 1873253.27, 2341346.09, 
2442022.48, 2530517.69, 2782013.06, 2511303.35, 2745913.92, 3438797.32, 
0, 0, 0, 0, 0, 0, 1549849.2, 458322.22, 718616.27, 1109753.32, 
1017724.67, 2026358.46, 1962226.89, 1997402.05, 2263479.22, 2440781.13, 
2849171.92, 3252589, 3137856.24, 3564965.12, 2984359.05, 2852952.11, 
2112507.39, 2954769.13, 2630636.16, 2380388.06, 2318520.61, 2427464.59, 
2995370, 2412997.64, 2689632.82, 1942677.02, 1834021.04, 2247508.4, 
2369731.57, 2462409.59, 2718120.89, 2430538.52, 2688636.78, 3392755, 
3224926.48, 0, 0, 0, 0, 0, 1348175.64, 430661.74, 674424.77, 
1083233.98, 922799.04, 1947889.63, 1865960.21, 1859749.01, 2189887.93, 
2317208.97, 2677149.96, 3058651.31, 3102056.06, 3350904.08, 2871884.58, 
2714588.23, 2030941.81, 2886763.56, 2545676.4, 2203422.72, 2218635.41, 
2336157.9, 2890812.15, 2363224.45, 2585735.04, 1870415.1, 1772222.84, 
2192869.99, 2325864.39, 2430416.64, 2665751.01, 2363822.67, 2584946, 
3338650.08, 3183330.22, 2794029.57, 0, 0, 0, 0, 1134174.58, 394195.69, 
627591.36, 986996.97, 905094.24, 1869262.62, 1777335.43, 1831133.54, 
1998551.54, 2130413.1, 2530188.25, 2900885.28, 2989396.31, 3261043.88, 
2817926.06, 2585234.28, 1834641.19, 2692959.95, 2496464.5, 2147759.85, 
2192197.19, 2254492.51, 2801209.29, 2225989.08, 2401570.87, 1786064.52, 
1728411.08, 2117010.38, 2282669.14, 2302139, 2535140.61, 2300978.22, 
2509151.42, 3222196.24, 3102535.64, 2728099.89, 3003458.16, 0, 
0, 0, 1001562.98, 376913.78, 579999.58, 942360.65, 837596.21, 
1777996.71, 1690021.16, 1747746.57, 2052779.77, 2088799.47, 2424260.53, 
2778369.19, 2857931.91, 3142942.88, 2635443.64, 2547750.85, 1822496.3, 
2637954.65, 2452785.43, 2025615.08, 2109856.44, 2262505.43, 2715446.67, 
2172158.39, 2366269.14, 1735742.65, 1662893.95, 2038204.81, 2191816.57, 
2240601.75, 2468849.08, 2234805.75, 2423405.74, 3168668.95, 3035690.96, 
2636414.45, 2919034.44, 2486931.57, 0, 0, 874341.22, 360782.57, 
519507.21, 870288.09, 786998.61, 1663642.58, 1550763.45, 1669917.52, 
1945941.78, 1970075.62, 2329146.26, 2585321.62, 2656298.81, 3029946.39, 
2490842.99, 2435587.25, 1739728.8, 2580447.13, 2258039.32, 1937348.09, 
2013383.14, 2146847.21, 2651523.71, 2053389.25, 2223539.76, 1658829.44, 
1577862.53, 1958701.51, 2122630.01, 2237987.33, 2447107.3, 2145457.33, 
2367896.01, 3090013.67, 2979750.06, 2546079.35, 2850658.25, 2447687.12, 
2515076.46, 0, 727314.1, 342974.57, 488469.13, 783815.37, 723097.48, 
1571210.47, 1459534.22, 1500041.34, 1810510.32, 1871554.2, 2124891.18, 
2465003.74, 2589953.19, 2908070.17, 2328456.61, 2374480.31, 1611237.61, 
2423377.37, 2160764.18, 1819919.89, 1945118.58, 2044143.09, 2478541.13, 
1995214.4, 2107312.32, 1617642.92, 1539948.97, 1915128.43, 2013819.45, 
2106946.21, 2366282.4, 2048390.08, 2320985.47, 2933079.27, 2923955.09, 
2496980.3, 2786647.48, 2380798.87, 2479884.64, 2674568.38), .Dim = c(40L, 
40L), .Dimnames = list(c("jul 10", "ago 10", "sep 10", "oct 10", 
"nov 10", "dic 10", "ene 11", "feb 11", "mar 11", "abr 11", "may 11", 
"jun 11", "jul 11", "ago 11", "sep 11", "oct 11", "nov 11", "dic 11", 
"ene 12", "feb 12", "mar 12", "abr 12", "may 12", "jun 12", "jul 12", 
"ago 12", "sep 12", "oct 12", "nov 12", "dic 12", "ene 13", "feb 13", 
"mar 13", "abr 13", "may 13", "jun 13", "jul 13", "ago 13", "sep 13", 
"oct 13"), c("jul 10", "ago 10", "sep 10", "oct 10", "nov 10", 
"dic 10", "ene 11", "feb 11", "mar 11", "abr 11", "may 11", "jun 11", 
"jul 11", "ago 11", "sep 11", "oct 11", "nov 11", "dic 11", "ene 12", 
"feb 12", "mar 12", "abr 12", "may 12", "jun 12", "jul 12", "ago 12", 
"sep 12", "oct 12", "nov 12", "dic 12", "ene 13", "feb 13", "mar 13", 
"abr 13", "may 13", "jun 13", "jul 13", "ago 13", "sep 13", "oct 13"
)))

Thanks for your help.

like image 934
Duck Avatar asked Jan 05 '14 20:01

Duck


People also ask

How do you expand a color palette?

Make different shapes with each color and see how they look together. If you have some past design projects, try adding your new colors into the mix. See what works, what doesn't, or what needs to be tweaked. Try using your brand font in different colors with a variety of backgrounds as well.

How do I change the color scheme in R?

Navigate to Tools → Global options → Appearance and switch the theme in the Editor Theme option. By default, you will have the Textmate theme activated. There is a wide in-built variety of themes to choose, from light to dark themes.

What is the default Ggplot color palette?

By default, ggplot2 chooses to use a specific shade of red, green, and blue for the bars.

Is Ggplot colorblind friendly?

The default colors in ggplot2 can be difficult to distinguish from one another because they have equal luminance. They are also not friendly for colorblind viewers.


1 Answers

Set3 has 12 distinct colors (see ?RColorBrewer). If you want more, you need to interpolate:

library(grid) # for unit()
cols <- colorRampPalette(brewer.pal(12, "Set3"))
myPal <- cols(length(unique(Melt.Atotal$Mes)))
Atotal <- ggplot(Melt.Atotal, aes(x = Var2, y = value, fill = Mes)) +
  geom_bar(stat = "identity") + 
  scale_fill_manual(values = myPal) +
  theme(axis.text.x=element_text(angle=90), 
        legend.text=element_text(size=8),
        legend.key.height=unit(.1, "cm"))
Atotal

enter image description here

like image 50
lukeA Avatar answered Sep 24 '22 16:09

lukeA