How to color a geom_bar by the variables intensity

Hi

I have a dataset below, I am making a bar graph with x=Continent, y=Count and color the bar graph by the intensity of footprint.I made code like this

-my code
w<-read.csv(file="Satisfaction.csv",head=TRUE,sep=",")
p<-ggplot(data=w, aes(x=Continent, fill=Continent))
p+geom_bar(color= “blue”)
p+geom_bar(width=.5)

print(p + geom_bar(width=.5)+ggtitle(“Jielan Liu Question 13”) + theme(plot.title=element_text(hjust=0.5))

  • dataset
    ID Country Continent LE WellBeing Footprint HPI
    109 Afghanistan Asia 48.7 4.8 0.5 36.8
    18 Albania Europe 76.9 5.3 1.8 54.1
    26 Algeria Africa 73.1 5.2 1.6 52.2
    127 Angola Africa 51.1 4.2 0.9 33.2
    17 Argentina South America 75.9 6.4 2.7 54.1
    53 Armenia Europe 74.2 4.4 1.7 46
    76 Australia Ocenia 81.9 7.4 6.7 42
    48 Austria Europe 80.9 7.3 5.3 47.1
    80 Azerbaijan Europe 70.7 4.2 2 40.9
    146 Bahrain Asia 75.1 4.5 6.6 26.6
    11 Bangladesh Asia 68.9 5 0.7 56.3
    103 Belarus Europe 70.3 5.5 4 37.4
    107 Belgium Europe 80 6.9 7.1 37.1
    4 Belize Europe 76.1 6.5 2.1 59.3
    132 Benin Africa 56.1 3.7 1.4 31.1
    64 Bolivia South America 66.6 5.8 2.6 43.6
    74 Bosnia and Herzegovina Europe 75.7 4.7 2.7 42.4
    151 Botswana Africa 53.2 3.6 2.8 22.6
    21 Brazil South America 73.5 6.8 2.9 52.9
    123 Bulgaria Europe 73.4 4.2 3.6 34.1
    129 Burkina Faso Africa 55.4 4 1.5 31.8
    135 Burundi Africa 50.4 3.8 0.8 30.5
    85 Cambodia Asia 63.1 4.2 1.2 40.3
    124 Cameroon Africa 51.6 4.4 1.1 33.7
    65 Canada North America 81 7.7 6.4 43.6
    148 Central African Republic Africa 48.4 3.6 1.4 25.3
    150 Chad Africa 49.6 3.7 1.9 24.7
    19 Chile South America 79.1 6.6 3.2 53.9
    60 China Asia 73.5 4.7 2.1 44.7
    3 Colombia South America 73.7 6.4 1.8 59.8
    112 Comoros Africa 61.1 3.9 1.3 36.5
    121 Congo Africa 57.4 3.8 1.1 34.5
    134 Congo, Dem. Rep. of the Africa 48.4 4 0.8 30.5
    1 Costa Rica South America 79.3 7.3 2.5 64
    113 Cote d'Ivoire Africa 55.4 4.2 1 35.9
    82 Croatia Europe 76.6 5.6 4.2 40.6
    12 Cuba South America 79.1 5.4 1.9 56.2
    59 Cyprus Europe 79.6 6.4 4.4 45.5
    92 Czech Republic Europe 77.7 6.2 5.3 39.4
    110 Denmark Europe 78.8 7.8 8.3 36.6
    106 Djibouti Africa 57.9 5 1.8 37.2
    33 Dominican Republic South America 73.4 4.7 1.4 50.7
    23 Ecuador South America 75.6 5.8 2.4 52.5
    91 Egypt Africa 73.2 3.9 2.1 39.6
    5 El Salvador South America 72.2 6.7 2 58.9
    117 Estonia Europe 74.8 5.1 4.7 34.9
    94 Ethiopia Africa 59.3 4.4 1.1 39.2
    70 Finland Europe 80 7.4 6.2 42.7
    50 France Europe 81.5 6.8 4.9 46.5
    55 Georgia Europe 73.7 4.1 1.4 46
    46 Germany Europe 80.4 6.7 4.6 47.2
    86 Ghana Africa 64.2 4.6 1.7 40.3
    83 Greece Europe 79.9 5.8 4.9 40.5
    10 Guatemala South America 71.2 6.3 1.8 56.9
    137 Guinea Africa 54.1 4 1.7 30
    31 Guyana South America 69.9 6 2.1 51.2
    78 Haiti North America 62.1 3.8 0.6 41.3
    13 Honduras South America 73.1 5.9 1.7 56
    102 Hong Kong Asia 82.8 5.6 5.8 37.5
    104 Hungary Europe 74.4 4.7 3.6 37.4
    88 Iceland Europe 81.8 6.9 6.5 40.2
    32 India Asia 65.4 5 0.9 50.9
    14 Indonesia Asia 69.4 5.5 1.1 55.5
    77 Iran Asia 73 4.8 2.7 41.7
    36 Iraq Asia 69 5 1.4 49.2
    73 Ireland Europe 80.6 7.3 6.2 42.4
    15 Israel Asia 81.6 7.4 4 55.2
    51 Italy Europe 81.9 6.4 4.5 46.4
    6 Jamaica North America 73.1 6.2 1.7 58.5
    45 Japan Asia 83.4 6.1 4.2 47.5
    27 Jordan Asia 73.4 5.7 2.1 51.7
    119 Kazakhstan Asia 67 5.5 4.1 34.7
    98 Kenya Africa 57.1 4.3 0.9 38
    63 Korea Asia 80.6 6.1 4.6 43.8
    143 Kuwait Asia 74.6 6.6 9.7 27.1
    38 Kyrgyzstan Asia 67.7 5 1.3 49.1
    37 Laos Asia 67.5 5 1.3 49.1
    118 Latvia Europe 73.3 4.7 4 34.9
    69 Lebanon Asia 72.6 5.2 2.8 42.9
    116 Liberia Africa 56.8 4.2 1.3 35.2
    81 Libya Africa 74.8 4.9 3.2 40.8
    120 Lithuania Europe 72.2 5.1 4.4 34.6
    138 Luxembourg Europe 80 7.1 10.7 29
    140 Macedonia Europe 74.8 4.2 5.4 28.3
    49 Madagascar Africa 66.7 4.6 1.2 46.8
    72 Malawi Africa 54.2 5.1 0.8 42.5
    84 Malaysia Asia 74.2 5.6 3.9 40.5
    147 Mali Africa 51.4 3.8 1.9 26
    66 Malta Europe 79.6 5.8 4.3 43.1
    128 Mauritania Africa 58.6 5 2.9 32.3
    111 Mauritius Africa 73.4 5.5 4.6 36.6
    22 Mexico North America 77 6.8 3.3 52.9
    40 Moldova Europe 69.3 5.6 2.1 48
    145 Mongolia Asia 68.5 4.6 5.5 26.8
    42 Morocco Africa 72.2 4.4 1.3 47.9
    114 Mozambique Africa 50.2 4.7 0.8 35.7
    61 Myanmar Asia 65.2 5.3 1.9 44.2
    96 Namibia Africa 62.5 4.9 2 38.9
    58 Nepal Asia 68.8 3.8 0.8 45.6
    67 Netherlands Europe 80.7 7.5 6.3 43.1
    28 New Zealand Europe 80.7 7.2 4.3 51.6
    8 Nicaragua South America 74 5.7 1.6 57.1
    144 Niger Africa 54.7 4.1 2.6 26.8
    125 Nigeria Africa 51.9 4.8 1.4 33.6
    29 Norway Europe 81.1 7.6 4.8 51.4
    16 Pakistan Asia 65.4 5.3 0.8 54.1
    30 Palestine Asia 72.8 4.8 1.4 51.2
    7 Panama North America 76.1 7.3 3 57.8
    57 Paraguay South America 72.5 5.8 3 45.8
    24 Peru South America 74 5.6 2 52.4
    25 Philippines Asia 68.7 4.9 1 52.4
    71 Poland Europe 76.1 5.8 3.9 42.6
    97 Portugal Europe 79.5 4.9 4.1 38.7
    149 Qatar Asia 78.4 6.6 11.7 25.2
    75 Romania Europe 74 4.9 2.8 42.2
    122 Russia Asia 68.8 5.5 4.4 34.5
    108 Rwanda Africa 55.4 4 0.7 36.9
    56 Saudi Arabia Asia 73.9 6.7 4 46
    126 Senegal Africa 59.3 3.8 1.5 33.3
    79 Serbia Europe 74.5 4.5 2.6 41.3
    139 Sierra Leone Africa 47.8 4.1 1.1 28.8
    90 Singapore Asia 81.1 6.5 6.1 39.8
    89 Slovakia Europe 75.4 6.1 4.7 40.1
    87 Slovenia Europe 79.3 6.1 5.2 40.2
    142 South Africa Africa 52.8 4.7 2.6 28.2
    62 Spain Europe 81.4 6.2 4.7 44.1
    35 Sri Lanka Asia 74.9 4.2 1.2 49.4
    101 Sudan Africa 61.5 4.4 1.6 37.6
    52 Sweden Europe 81.4 7.5 5.7 46.2
    34 Switzerland Europe 82.3 7.5 5 50.3
    47 Syria Asia 75.9 4.1 1.5 47.1
    43 Tajikistan Asia 67.5 4.4 0.9 47.8
    133 Tanzania Africa 58.2 3.2 1.2 30.7
    20 Thailand Asia 74.1 6.2 2.4 53.5
    141 Togo Africa 57.1 2.8 1 28.2
    136 Trinidad and Tobago North America 70.1 6.7 7.6 30.3
    39 Tunisia Africa 74.5 4.7 1.8 48.3
    44 Turkey Asia 74 5.5 2.6 47.6
    95 Turkmenistan Asia 65 6.6 4 39.1
    131 Uganda Africa 54.1 4.2 1.6 31.5
    100 Ukraine Europe 68.5 5.1 3.2 37.6
    130 United Arab Emirates Asia 76.5 7.2 8.9 31.8
    41 United Kingdom Europe 80.2 7 4.7 47.9
    105 United States of America North America 78.5 7.2 7.2 37.3
    93 Uruguay Africa 77 6.1 5.1 39.3
    54 Uzbekistan Asia 68.3 5.1 1.8 46
    9 Venezuela South America 74.4 7.5 3 56.9
    2 Vietnam Asia 75.2 5.8 1.4 60.4
    68 Yemen Asia 65.5 3.9 0.9 43
    99 Zambia Africa 49 5.3 0.8 37.7
    115 Zimbabwe Africa 51.4 4.8 1.2 35.3

This is not quite a reproducible example but have you tried "fill = Footprint"? I know you said color but color refers to the bars' borders - I am assuming you want the fill to reflect intensity.

So...

w<-read.csv(file="Satisfaction.csv",head=TRUE,sep=",")
p<-ggplot(data=w, aes(x=Continent, fill=Footprint)) +
     geom_bar()

If you mean make the color more or less transparent/opaque, and since Footprint is a continuous (not discrete) value, you can pass it to the alpha aesthetic like this:

ggplot(data=w, aes(x=Continent, alpha=Footprint))

You could still use continent as fill as you have it already.

ggplot(data=w, aes(x=Continent, fill = Continent, alpha=Footprint))
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.