barcharts in R studio

Hi,
I am trying to create a barchart about different types of funding per country. So my x-axis should represent the countries, the y axis is the amout of funding in Euros and the "fill" is the three different types of funding. my data set is set up in 4 colums the first one is are the country codes and the 2nd to 4th collums are the amounts of funding. So far we have not found a way to showcase the three types of funding per country next to eachother. We want a dodge bar graph. So the problem is that we want to represent three different variables per country in the "fill" element.
we tried this:
ggplot (pf_sorted, aes(x= GEO.SRC_FUND, y= amount, fill = factor(Public.funding,National.central.government,European.Union))) + geom_bar (stat= "identity", position = "dodge" )
Error in hue_pal(3) : could not find function "hue_pal"

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Hi, This is our data. I hope this is what you needed

data
GEO.SRC_FUND Public.funding National.central.government European.Union
1 BE 2375 1486 296
2 BG 1022 697 935
3 CZ 2981 1620 1928
4 DE 14823 10358 3702
5 EE 308 235 120
6 EL 1271 842 267
7 ES 4570 2952 1251
8 FR 14622 13487 1899
9 HR 538 330 173
10 IT 17311 10043 2102
11 CY 129 97 44
12 LV 263 44 242
13 LT 457 200 268
14 LU 172 162 31
15 HU 904 478 615
16 MT 51 41 15
17 NL 5326 5027 619
18 PL 2247 809 1533
19 PT 3028 2027 1244
20 RO 597 277 373
21 SI 361 283 174
22 SK 306 107 244
23 FI 1643 1363 292
24 SE 1223 857 457
ggplot (pf_sorted, aes(x= GEO.SRC_FUND, y= amount, fill = factor(Public.funding,National.central.government,European.Union))) + geom_bar (stat= "identity", position = "dodge")
Error: Must request at least one colour from a hue palette.

Sorry please try again.
Use dput (or datapasta)

data

GEO.SRC_FUND Public.funding National.central.government European.Union
1            BE           2375                        1486            296
2            BG           1022                         697            935
3            CZ           2981                        1620           1928
4            DE          14823                       10358           3702
5            EE            308                         235            120
6            EL           1271                         842            267
7            ES           4570                        2952           1251
8            FR          14622                       13487           1899
9            HR            538                         330            173
10           IT          17311                       10043           2102
11           CY            129                          97             44
12           LV            263                          44            242
13           LT            457                         200            268
14           LU            172                         162             31
15           HU            904                         478            615
16           MT             51                          41             15
17           NL           5326                        5027            619
18           PL           2247                         809           1533
19           PT           3028                        2027           1244
20           RO            597                         277            373
21           SI            361                         283            174
22           SK            306                         107            244
23           FI           1643                        1363            292
24           SE           1223                         857            457

ggplot (pf_sorted, aes(x= GEO.SRC_FUND, y= amount, fill = factor(Public.funding,National.central.government,European.Union))) + geom_bar (stat= "identity", position = "dodge")
Error: Must request at least one colour from a hue palette.

Ok... ill directly quote the relevant portion of the FAQ

You can also use dput provided in base , which is as simple as this:

dput(head(iris, 5)[c("Sepal.Length", "Sepal.Width")])

I am so sorry but I don't really understand what i have to post.

Your data is called pf_sorted
So post the result of

dput(pf_sorted)
pf_sorted <- structure(list(GEO.SRC_FUND = c("MT", "CY", "LU", "LV", "SK", 
"EE", "SI", "LT", "HR", "RO", "HU", "BG", "SE", "EL", "FI", "PL", 
"BE", "CZ", "PT", "ES", "NL", "FR", "DE", "IT"), Public.funding = c(51, 
129, 172, 263, 306, 308, 361, 457, 538, 597, 904, 1022, 1223, 
1271, 1643, 2247, 2375, 2981, 3028, 4570, 5326, 14622, 14823, 
17311), National.central.government = c(41, 97, 162, 44, 107, 
235, 283, 200, 330, 277, 478, 697, 857, 842, 1363, 809, 1486, 
1620, 2027, 2952, 5027, 13487, 10358, 10043), European.Union = c(15, 
44, 31, 242, 244, 120, 174, 268, 173, 373, 615, 935, 457, 267, 
292, 1533, 296, 1928, 1244, 1251, 619, 1899, 3702, 2102)), row.names = c(16L, 
11L, 14L, 12L, 22L, 5L, 21L, 13L, 9L, 20L, 15L, 2L, 24L, 6L, 
23L, 18L, 1L, 3L, 19L, 7L, 17L, 8L, 4L, 10L), class = "data.frame")

library(tidyverse)

(pf_sorted_long <- pivot_longer(pf_sorted, cols = -GEO.SRC_FUND,
                                values_to = "amount"))

ggplot(pf_sorted_long, aes(x = GEO.SRC_FUND, 
                           y = amount,
                           fill = name)) +
  geom_bar(stat = "identity", position = "dodge")

1 Like

thank you a lot this was great help

As always, I thought I might chime in with a {base} R solution,

First, here is some nice reproducible data for anyone interested in replicating this,

df <- data.frame(
             stringsAsFactors = FALSE,
                 GEO.SRC_FUND = c("BE",
                                  "BG","CZ","DE","EE","EL","ES","FR","HR",
                                  "IT","CY","LV","LT","LU","HU","MT","NL",
                                  "PL","PT","RO","SI","SK","FI","SE"),
               Public.funding = c(2375L,
                                  1022L,2981L,14823L,308L,1271L,4570L,14622L,
                                  538L,17311L,129L,263L,457L,172L,904L,
                                  51L,5326L,2247L,3028L,597L,361L,306L,
                                  1643L,1223L),
  National.central.government = c(1486L,
                                  697L,1620L,10358L,235L,842L,2952L,13487L,
                                  330L,10043L,97L,44L,200L,162L,478L,41L,
                                  5027L,809L,2027L,277L,283L,107L,1363L,
                                  857L),
               European.Union = c(296L,
                                  935L,1928L,3702L,120L,267L,1251L,1899L,
                                  173L,2102L,44L,242L,268L,31L,615L,15L,
                                  619L,1533L,1244L,373L,174L,244L,292L,457L)
)

And now the {base} R code,

barplot(as.matrix(df[-1]) ~ df$GEO.SRC_FUND,
        main = "Some appropriate title",
        xlab = "Geo Source Fund",
        ylab = "Dollars",
        col = 1:3,
        beside = TRUE)
legend("topright", legend = names(df[-1]), 
       fill = 1:3,
       cex = 0.75)

Created on 2020-09-16 by the reprex package (v0.3.0)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.