Creating a stacked or multi-variable bar graph.

So, I am having a problem with creating a bar graph that I can plot both of my variables of LifeEB17 which is life expectancy and Basic sanitation on the x-axis and present values on the y-axis. They are both percentage numbers. Please help!

data = HealthNutri
view (HealthNutri)

#Packages
library(ggplot2)
library(tidyr)
library(dplyr)

#Comparison of life expectancy and percentage of people using basic sanitation
HealthNutri <- HealthNutri[c(54, 83, 112, 130, 132, 137, 139, 140, 142, 148, 158, 160, 167, 191, 215, 223, 231, 232),]
HealthNutri <- data.frame(CountryName = c("Australia", "Channel Islands", "France", "Hong Kong SAR", "Iceland", "Ireland", "Israel", "Italy", "Japan", "North Korea", "Liechtenstein", "Luxembourg", "Malta", "Norway", "Singapore", "Spain", "Sweden","Switzerland"), LifeEB17=c(82.5, 83, 82.58, 84.68, 82.66, 82.16, 82.55, 82.94, 84.10, 82.63, 83.75, 82.10, 82.35, 82.61, 83.10, 83.28, 82.41, 83.55), BasicSanitation17 = c(100, 98.5, 98.65, 96.44, 98.78, 91.25, 100, 98.77, 99.89, 100, 99.95, 97.60, 99.96, 98.05, 100, 99.90, 99.29, 99.89))`

Your example data.frame has 19 elements in *BasicSanitation17 * and '8 in the other 2 columns.

It is not clear what your first statement

HealthNutri <- HealthNutri[c(54, 83, 112, 130, 132, 137, 139, 140, 142, 148, 158, 160, 167, 191, 215, 223, 231, 232),]

is intended to do. Are you trying to subset data.frame below it?

Each of my variables (BasicSanitation17 and LifeEB17 have 18 values attached to it). My CountryName variable is just the name of the countries corresponding to those 18 values of the variables mentioned above. What I am looking to do is graph values on the Y axis, but plot the LifeEB17 and BasicSanitation17 on the x-axis.

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.

I still am not understanding what you want when I look at your code.

For the data.frame HealthNutri that you have supplied this will give you a barplot

ggplot(HealthNutri, aes(CountryName, LifeEB17)) +
    geom_bar(stat = "identity")

If you wish to have both LifeEB17 and BasicSanitation17 on the same graph in ggplot2 then you will need to change the data. frame from wide to long format .

I think we need to see some sample data in a usable format. Please have a look at