How can I do a grouping bar with the following data?

I have the following information:

library(readr)
test <- read_csv("test.csv")
View(test)

test

A tibble: 25 x 3

Country Periods Value

1 US Gold Standard 1881-1913 0.3
2 US Interwar 1919-1938 -1.8
3 US Bretton Woods 1946-1970 2.4
4 US Floating Exchange 1974-1989 5.6
5 US Floating Exchange 1990-2019 2.4
6 UK Gold Standard 1881-1913 0.3
7 UK Interwar 1919-1938 -1.5
8 UK Bretton Woods 1946-1970 3.7
9 UK Floating Exchange 1974-1989 9.4
10 UK Floating Exchange 1990-2019 2.5

… with 15 more rows

And I want to do a grouping bar in studio but I done know how to do it,
it is something like the photo

I really appreciate if you can help me

This is an example

library(ggplot2)

# Sample data on a copy/paste friendly format (replace this with your actual data frame)
sample_df <- data.frame(
  stringsAsFactors = FALSE,
           Country = c("US","US","US","US","US",
                       "UK","UK","UK","UK","UK"),
           Periods = c("Gold Standard 1881-1913",
                       "Interwar 1919-1938","Bretton Woods 1946-1970",
                       "Floating Exchange 1974-1989","Floating Exchange 1990-2019",
                       "Gold Standard 1881-1913","Interwar 1919-1938",
                       "Bretton Woods 1946-1970","Floating Exchange 1974-1989",
                       "Floating Exchange 1990-2019"),
             Value = c(0.3, -1.8, 2.4, 5.6, 2.4, 0.3, -1.5, 3.7, 9.4, 2.5)
)

# Relevant code
ggplot(sample_df, aes(x = Country, y = Value, fill = Periods)) +
    geom_col(position = "dodge")

Created on 2020-12-07 by the reprex package (v0.3.0.9001)

Note: Next time please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Sorry but it dos not work and I don't know what to do, I write the following code

library(readr)
test <- read_csv("test.csv")
View(test)
sample_df <- data.frame(
stringsAsFactors = FALSE,
Country = c(test$Country)
Periods= c(test$Periods)
Value= c(test$Value)
ggplot(sample_df, aes(x = Country, y = Value, fill = Periods)) +
geom_col(position = "dodge")

There are 5 more countries I also tried this way

sample_df <- data.frame(
stringsAsFactors = FALSE,
Country = c("US","US","US","US","US",
"UK","UK","UK","UK","UK",
"FR","FR","FR","FR","FR",
"DE","DE","DE","DE","DE",
"JP","JP","JP","JP","JP"),
Periods = c("Gold Standard 1881-1913",
"Interwar 1919-1938","Bretton Woods 1946-1970",
"Floating Exchange 1974-1989","Floating Exchange 1990-2019",
"Gold Standard 1881-1913","Interwar 1919-1938",
"Bretton Woods 1946-1970","Floating Exchange 1974-1989",
"Floating Exchange 1990-2019",
"Gold Standard 1881-1913",
"Interwar 1919-1938","Bretton Woods 1946-1970",
"Floating Exchange 1974-1989","Floating Exchange 1990-2019",
"Gold Standard 1881-1913",
"Interwar 1919-1938","Bretton Woods 1946-1970",
"Floating Exchange 1974-1989","Floating Exchange 1990-2019",
"Gold Standard 1881-1913",
"Interwar 1919-1938","Bretton Woods 1946-1970",
"Floating Exchange 1974-1989","Floating Exchange 1990-2019",),
Value = c(0.3, -1.8, 2.4, 5.6, 2.4, 0.3, -1.5, 3.7 , 9.4, 2.5, 0, 2.2, 5.6, 8.8, 1.6, 0.6,-2.1,2.7,2.6,1.8,4.6,-1.9,4.5,4.2,0.5))

Relevant code

ggplot(sample_df, aes(x = Country, y = Value, fill = Periods)) +
geom_col(position = "dodge")

You have an extra comma after the very last "Floating Exchange 1990-2019".

When I ran your code it gave the message:

Error in c("Gold Standard 1881-1913", "Interwar 1919-1938", "Bretton Woods 1946-1970", :
argument 26 is empty

Rather than say "it does not work" it would be very helpful if you reported any error messages. Even better, provide a reprex.

Yea you ar right it worked!!!!!thank you very much

You don't need to write your data this way, I did it just for reproducibility purposes (read the link I gave you about "reproducible examples" to understand why), you can use the data frame that you get by reading the csv file.

This topic was automatically closed 21 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.