-2

I'm struggling with how to do something with R that comes very easily to me in Excel: so I'm sure this is something quite basic but I'm just not aware of the equivalent method in R.

In essence, I have a two variables in my dataset: a categorical variable which has a list of names, and an analytical variable that has the frequency corresponding to that particular observation.

Something like this:

Name Freq
==== =========
X         100
Y         200

and so on.

I would like to plot a bar chart with the names listed on the X-Axis (X, Y and so on) and bars of height corresponding to the relevant value of the Freq. variable for that observation.

This is something very trivial with Excel; I can just select the relevant cells and create a bar chart. However, in R I just can't seem to figure out how to do this! The bar charts in R seems to be univariate only and doesn't behave the way I want it to. Trying to plot the two variables results in a scatter plot which is not what I'm going for.

Is there something very basic I'm missing here, or is R just not capable of performing this task?

Any pointers will be much helpful.

Edited to Add: I was primarily trying to use base R's plot function to get the job done.

Using, plot(dataset1$Name, dataset1$Freq) does not lead to a bar graph but a scatter-plot instead.

  • 1
    What commands exactly where you trying in R? Are you using base graphics or ggplot2? When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 20 '18 at 15:53
  • 1
    Lots of examples here: https://ggplot2.tidyverse.org/reference/geom_bar.html – MrFlick Jul 20 '18 at 15:53

1 Answers1

1

First the data.

dat <- data.frame(Name = c("X", "Y"), Freq = c(100, 200))

With base R.

barplot(dat$Freq, names.arg = dat$Name)

If you want to display a long list of names.arg, maybe the best way is to customize your horizontal axis with function staxlab from package plotrix. Here are two example plots.

One, with the axis labels rotated 45 degrees.

set.seed(3)

Name <- paste0("Name_", LETTERS[1:10])
dat2 <- data.frame(Name = Name, Freq = sample(100:200, 10))

bp <- barplot(dat2$Freq)
plotrix::staxlab(1, at = bp, labels = dat2$Name, srt = 45)

Another, with the labels spread over 3 lines.

bp <- barplot(dat2$Freq)
plotrix::staxlab(1, at = bp, labels = dat2$Name, nlines = 3)

Add colors with argument col. See help("par").

With ggplot2.

library(ggplot2)

ggplot(dat, aes(Name, Freq)) +
  geom_bar(stat = "identity")

To add colors you have the aesthetics colour (for the contour of the bars) and fill (for the interior of the bars).

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • So names.arg was what I was missing! Also, any idea how to get all the levels in names.arg to display properly? I have 35 levels in names.arg, and the default alignment can't display all of them in the chart. (Excel, eg, does this automatically by aligning the names at an angle, etc) – ABHISHEK CHAKRABARTI Jul 21 '18 at 05:22
  • @ABHISHEKCHAKRABARTI Done, see the second `barplot`. – Rui Barradas Jul 21 '18 at 09:14