1

So I used the plotrix library to plot a histogram using some weights , the histogram shows up as expected but when I tried a plot the mean as a vertical line it won't show up at all
Here's a snippet of my code:

library("plotrix")
library("zoom")


vals = seq.int(from = 52.5 , to = 97.5 , by = 5)
weights <- c(18.01,18.26,16.42,14.07,11.67,9.19,6.46,3.85,1.71,0.34)/100

mean <- sum(vals*weights)
wh <- weighted.hist(x = vals , w = weights , freq = FALSE)
abline(v = mean)

the abline() seems to work only with the normal hist() function

I am sorry if the question sounds stupid , I am R newbie however I did my research and could not find any helpful info.

Thanks in advance.

AmirWG
  • 251
  • 1
  • 2
  • 10
  • To give a [good reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), it is helpful to provide some data via `dput()`. – AndrewGB Jul 12 '21 at 03:21

1 Answers1

2

You should provide a sample of your data. Your calculation of the weighted mean is only correct if your weights sum to 1. If they do not, you should use weighted.mean(vals, weights) or sum(vals * weights/sum(weights)). The following example is slightly modified from the one on the weighted.hist manual page (help(weighted.hist)):

vals <- sample(1:10, 300, TRUE)
weights <- (101:400)/100
weighted.hist(vals, weights, breaks=1:10, main="Test weighted histogram")
(mean <- weighted.mean(vals, weights))
# [1] 5.246374

The histogram starts at 1, but this is 0 on the x-axis coordinates so we need to subtract 1 to get the line in the right place:

abline(v=mean-1, col="red")

Histogram

Using your data we need to identify the first boundary to adjust the mean so it plots in the correct location"

wh$breaks[1]
# [1] 52.5
abline(v=mean - wh$breaks[1], col="red")

New Histogram

dcarlson
  • 10,936
  • 2
  • 15
  • 18
  • my weights sum up to 0.9998 but it's no big deal since it's so close to 1 , how would that affect printing the vertical line?, btw why did you create 299 weights instead of 300? (400-101 = 299) since you had your sample size = 300 – AmirWG Jul 12 '21 at 03:44
  • and as i mentioned above i am sure the problem has nothing do with the data set itself but rather weighted.hist() since i tried hist() with a test data set and i can plot the vertical line just fine – AmirWG Jul 12 '21 at 03:48
  • `length(101:400)` is 300 just like 1:10 is 10 values, not 9. Since you provided no data, it is impossible to say why you did not get a line. Since the axis coordinates start at 0, but your first histogram bar probably started at a number greater than that, it may have plotted outside the figure. – dcarlson Jul 12 '21 at 03:51
  • fine then i will provide the dataset – AmirWG Jul 12 '21 at 03:53
  • ohh i get it now , one last thing , does R always have the x-axis start at 0? – AmirWG Jul 12 '21 at 04:06
  • and of course thx a lot for your help much appreciated – AmirWG Jul 12 '21 at 04:15
  • 1
    No. The programmer sets the coordinates for the plot window. Generally they do match the range of the data, e.g. the `hist` and `plot` functions. You can always retrieve them with `par("usr")`. For your data that returns -1.8000000 46.8000000 0.0000000 0.3990498 so the vertical axis is positioned at -1.8 on the horizontal axis and the first bar starts at 0. – dcarlson Jul 12 '21 at 04:18