0

Here is what I have so far. I have to get my graph (in the picture) to look like the bottom picture! How can I get the order of the countries, title, and color like the picture?

  • Have the countries and their corresponding data go in order of (New Zeland, Taiwan, Australia, Iceland, United Kingdom, Canada, Vietnam, Italy, India, Japan, Germany, United States, Estonia, Indonesia, Philippines, South Africa, Serbia, Argentina, Tunisia, Ecuador) because New Zeland has the highest number and Ecuador has the lowest number.
  • Change the color of each bar
  • Add title

ANY SPECIFIC CODE YOU CAN PROVIDE AS IT RELATES TO MY DATASET WILL BE MUCH APPRECIATED!

View project_data_1 Data Set

View(ProjectData_1)

Install and Load Packages

library(ggplot2)
library(sjstats)
library(car)
library(pwr)

Labeling Variables

Countries <- ProjectData_1$Entity
TestPerCase <- ProjectData_1$cumulative_tests_per_case

Create New DataFrame

Countries <- c("New Zeland", "Taiwan", "Australia", "Iceland", "United Kingdom", "Canada", "Vietnam", "Italy",
               "India", "Japan", "Germany", "United States", "Estonia", "Indonesia","Philippines", "South Afria", "Serbia",
               "Argentina", "Tunisia", "Ecuador")
TestPerCase <- c(500, 250, 250, 41.7, 32.3, 27, 26.3, 23.8, 18.5, 15.4, 14.7, 13.7, 10.5, 8.7, 8.1, 6.5, 5.4, 4.9, 4.4, 4.6)
NewProjectData1 <-data.frame(Countries, TestPerCase)
print(NewProjectData1)

Create Bar Graph

Data1BarGraph <- ggplot (NewProjectData1, aes(x=Countries, y=TestPerCase)) +
  geom_bar(stat = "identity")
Data1BarGraph + coord_flip()

I have to get my graph (the top one) to look like the bottom graph-->

  • 1
    You have "TestPerCase" defined as a vector of character strings. Remove the quotes. `TestPerCase <- c(500, 250, 250,...)` – Dave2e Dec 04 '21 at 19:59
  • @Dave2e Wow... I totally feel like an idiot. How did I not notice this... that's what I get for working on this at 3 AM... THANK YOU for noticing this! – vanaynay25 Dec 04 '21 at 20:03
  • @Dave2e I have tried looking at that question but nothing seems to be working.. would you be able to provide me an example to get started? Thank you! – vanaynay25 Dec 04 '21 at 21:25

1 Answers1

0

As per reference, Order Bars in ggplot2 bar graph, to order the country values other than alphabetical, one needs to convert into factors, specifying the order.

Countries <- c("New Zeland", "Taiwan", "Australia", "Iceland", "United Kingdom", "Canada", "Vietnam", "Italy",
               "India", "Japan", "Germany", "United States", "Estonia", "Indonesia","Philippines", "South Afria", "Serbia",
               "Argentina", "Tunisia", "Ecuador")
TestPerCase <- c(500, 250, 250, 41.7, 32.3, 27, 26.3, 23.8, 18.5, 15.4, 14.7, 13.7, 10.5, 8.7, 8.1, 6.5, 5.4, 4.9, 4.4, 4.6)

#create dataframe
NewProjectData1 <-data.frame(Countries, TestPerCase)

#Convert Countries into a factors, ordered by TestPerCase value
NewProjectData1$Countries <- factor(NewProjectData1$Countries, 
                                    levels= NewProjectData1$Countries[order(NewProjectData1$TestPerCase)])

#Plot
#geom_col instead of geom_bar, avoids the extra 'stat = "identity"'
Data1BarGraph <- ggplot(NewProjectData1, aes(x=Countries, y=TestPerCase, fill=Countries)) +
   geom_col() +  
   coord_flip() +
   labs(title = "TestperCase vs Country")

Data1BarGraph
Dave2e
  • 22,192
  • 18
  • 42
  • 50