0

I want to create a histogram plot with ggplot starting from a frequency table. The freuqency table is the following:

Income Frequency
0-10 60
10-30 50
30-60 40
60-100 10

I create:

  • a data.frame of the data es1_=data.frame(income=c(0,10,30,60,100),VALUE=c(0,60,50,40,10),DENSITY=c(0,60/10,50/20,40/30,10/40))

  • the break vector intervalli=c(0,10,30,60,100)

  • a histogram es1_ %>% ggplot()+ geom_histogram(aes(x=INCOME,weight=DENSITY),breaks=intervalli)

enter image description here

Is it possible obtain the same histogram without edit manualy the DENSITY data? How can obtain the same plot with the geom_histogram standard option? Thank you in advance.

Fra_garch
  • 1
  • 1
  • If you have unequal bins, a better chart type would probably just be a bar chart. With your data in the current format it's a bit messy to get rid of the extra row, but you can do `with(es1_, data.frame(group = paste0(income[-length(income)],"-",income[-1]),value = VALUE[-1])) |>ggplot() + aes(group, value/sum(value)) + geom_col()` – MrFlick Mar 26 '22 at 23:08
  • Thanks MrFlick. With this approach I obtain a different plot and I lost the density property of the histogram's bins. – Fra_garch Mar 26 '22 at 23:27
  • 1
    I'm not entirely sure what your method is for calculating the density, but you could implement it mathematically instead of writing the values manually: `data.frame(INCOME=c(0,10,30,60,100),VALUE=c(0,60,50,40,10)) %>% mutate(DENSITY = replace_na(VALUE / ((row_number() - 1) * 10), 0))` – jdobres Mar 27 '22 at 00:09
  • 1
    I can't figure out your definition of density. either. Normally you scale the density to the area under all the bars is 1. Here if you did length*width for each of the bars and added them together you would get 160 – MrFlick Mar 27 '22 at 00:22
  • I am unable to find any pattern to calculate density. Can you provide more information? – Death Metal May 17 '22 at 04:47

0 Answers0