DenisK
DenisK

Reputation: 140

R: How to make histogram basing on the values in the other column as well?

Let's say I have a large dataset consisting of two columns.

The first one mentions different people (marking them with their name), while the second one is just a binary variable marking if a person mentioned in the first column was met in another dataset (it doesn't matter now in which one).

So I have something like this:

Name       Found

Peter      0

John       1

Peter      1

Mark       0

Peter      0

and so on.

I'd like to make a histogram representing: 1) the overall frequency for each name; 2) but the chart representing each name would be split into two parts by colour: found vs unfound. Something like this, actually: https://www.flickr.com/photos/gommit/6748028567, but having only two colours.

What's the best way to do so?

Upvotes: 1

Views: 100

Answers (1)

JasonAizkalns
JasonAizkalns

Reputation: 20463

Assuming your data is in a dataframe called df, you could use table and barplot to do something like:

barplot(table(df$Found, df$Name))

enter image description here

Upvotes: 1

Related Questions