-2

I am trying to plot a CSV file in GGPLOT in R, but I'm still very new to R…

The goal is to plot a bar chart with countries on one axis (Column A(Country) and a comparison of 4 values (Columns b-e (col.b, col.c, col.d, col.e)

right now I have:

library(ggplot2)

dataset <- read.csv("[path]/dataset.csv")

ggplot(data=dataset, aes (x=Country, y=col.b, col.c, col.d, col.e, fill=)) +
  geom_col(stat="identity") + 
  scale_fill_manual("legend", values = c("black", "orange", "blue", "green" ))

I seem to have trouble to get it to actually colour the bars for me and also it also shows col.b as far as I can tell. How should it look instead?

Jannik
  • 1
  • 2
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, valid code, and a clear explanation of what you're trying to do and what hasn't worked. We can't run your code without data and can't see what you're seeing. If the issue is reshaping data into a format ggplot expects, there are a lot of SO posts already—what about those hasn't helped? – camille Jan 01 '20 at 19:02
  • I'd also recommend that, for a general "how do I use this package?" type of question, introductory tutorials are a better place to start than SO, where we expect specific debugging questions. ggplot's docs are fairly thorough and include suggested tutorials such as [this one](https://r4ds.had.co.nz/data-visualisation.html) – camille Jan 01 '20 at 19:05

1 Answers1

-1

Based on the info provided, the columns are in 'wide' format, which can be reshaped to 'long' format (pivot_longer from tidyr) and then create the barplot

library(dplyr)
library(tidyr)
library(ggplot2)
dataset %>% 
   pivot_longer(cols = starts_with('col')) %>%
   ggplot(aes(x = Country, y = value, fill = name)) +
       geom_col() 

data

set.seed(24)
dataset <- data.frame(Country = rep(c("USA", "India"), each = 10), 
     col.b = sample(1:50, 20, replace = TRUE),
     col.c = sample(1:25, 20, replace = TRUE),
     col.d = sample(1:75, 20, replace = TRUE), 
     col.e = sample(1:40, 20, replace = TRUE), stringsAsFactors = FALSE)
akrun
  • 874,273
  • 37
  • 540
  • 662