0

I am new to R and ggplot. I have a set of large numbers and it seems that ggplot can't display it properly either with geom_bar() or geom_line(). Or maybe I missed a few parameters that can adjust the scale of the plot. Please point it out. Thank you!

For bar chart:

Command:

ggplot(income_exercise, aes(x= 'income2')) + geom_bar()

Data: data of income category and count of people in each category

Problematic plot:

each bar is oversized, ggplot can't adjust the scale automatically

For line chart:

command: ggplot(income_exercise, aes(x= 'income2', y="cat_count")) + geom_line()

problematic chart:

not display any data

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
  • Why `'income2'` in quotes? There is no need as such. Also `geom_bar` should have parameter `stat ="identity"` – MKR Apr 01 '18 at 16:04
  • Do not put your example data in as a picture. Please read [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a reproducible example. – phiver Apr 01 '18 at 16:07
  • Or, rather than `geom_bar`, use `geom_col` which automatically uses `stat="identity"` –  Apr 01 '18 at 16:23

1 Answers1

0

Hopefully this helps.

library(ggplot2)
df <- data.frame(income=c('Less than 10k', 
                           'Less than 15k',
                           'Less than 20k'),
                 freq=c(25441,
                          26794,
                          34873))
# for bar chart
ggplot(df, aes(x=income, y=freq)) + geom_bar(stat="identity")

# for lines
ggplot(df, aes(x=income, y=freq, group=1)) + geom_line() + geom_point()
Will
  • 812
  • 3
  • 11
  • 21