MKas
MKas

Reputation: 25

Bar Chart - Histogram in R

I want to plot a histogram of my dataset which contains two columns, one with names and the other one with values which looks like that:

 City      Values
 Graz       2799
 Innsbruck  2802
 Klagenfurt 2591
 Linz       3016
 Salzburg   2716
 Wien       2252

I want to create a bar histogram which shows how much in the value for each one of the names having the Names variable in the horizontal axis. I've tried the following code:

ggplot(data) +
  geom_bar(aes(x=City))

but what I am getting back is not what I want and it looks like that:

bar histogram

Do you know how I can fix that? Or do you have any suggestions about any other plot which will show me a similar result as what I want?

Upvotes: 0

Views: 589

Answers (2)

akrun
akrun

Reputation: 887118

In base R, we can use barplot

barplot(Values ~ City, df1)

-output

enter image description here

data

df1 <- structure(list(City = c("Graz", "Innsbruck", "Klagenfurt", "Linz", 
"Salzburg", "Wien"), Values = c(2799L, 2802L, 2591L, 3016L, 2716L, 
2252L)), class = "data.frame", row.names = c(NA, -6L))

Upvotes: 0

pascal
pascal

Reputation: 1085

Here comes the barplot with your values.

ggplot(data, aes(x=City, y=Values)) +
     geom_bar(stat = "identity")

enter image description here

From the docs:

By default, geom_bar uses stat="bin" . This makes the height of each bar equal to the number of cases in each group, and it is incompatible with mapping values to the y aesthetic. If you want the heights of the bars to represent values in the data, use stat="identity" and map a value to the y aesthetic.

Upvotes: 1

Related Questions