This question already has answers here:
Why does the hist() function not have area one
(4 answers)
Closed 1 year ago.
I am trying to plot an histogram using R.
I decided to use the function hist() but I cannot understand why by changing the "breaks" option the sum of the density also changes.
In fact if I write
h <- hist(data, freq =F, breaks = "FD")
and then run
sum(h$density)
the result is 2 (same thing for breaks = "Scott"). While if I use
h <- hist(data, freq =F)
the result is 1 (as expected).
Summing the density values only makes sense if your bins are one unit wide. You want to sum the areas of the bars, which is the density value times the bin width. Presumably your FD bins are half the width of the default bins.
Related
Lets say I have 10 observations of 200 points of integers between one and ten:
mysample = sample(rep(seq(1,10),20),10);
and I want to barplot it
barplot(table(mysample));
barplot
In this example, there are no observations of 7. Is there a quick way of telling barplot to set the x-axis range to all integers between 1 and 10, or do I have to manually edit the table?
Try
barplot(table(factor(mysample, levels=1:10)));
By using a factor, R will know which levels are "missing"
This question already has an answer here:
change plotted segment ends from round to flat
(1 answer)
Closed 5 years ago.
I made a histogram plot in R using plot(type = 'h').
plot(type='h', y =runif(1:10), x = 1:10,lwd = 20,lty=7)
I want the vertical bars to be thick, so I increase lwd.
However, the larger "bars" (really, they're lines) have rounded edges:
Is there a way to make these "bars" (i.e., lines) have square/straight edges?
I've gone through the plot documentation numerous times and couldn't find a solution. No SE or Google Searches turned up anything either...
Use lend = 1.
plot(type='h', y =runif(1:10), x = 1:10,lwd = 20,l ty = 7, lend = 1)
And output
This question already has answers here:
Set R plots x axis to show at y=0
(2 answers)
Closed 3 years ago.
I observed that the x axes of a plot doesn't cross the y axes at 0.
Why?
How can I fix that?
Example:
plot(mtcars$mpg, ylim=c(0,50))
By default, R extends the axes by 4% on either end around the limits: from ?par,
‘xaxs’ The style of axis interval calculation to be used for the
x-axis. Possible values are ‘"r"’, ‘"i"’, ‘"e"’, ‘"s"’,
‘"d"’. The styles are generally controlled by the range of
data or ‘xlim’, if given.
Style ‘"r"’ (regular) first extends the data range by 4
percent at each end and then finds an axis with pretty labels
that fits within the extended range.
Style ‘"i"’ (internal) just finds an axis with pretty labels
that fits within the original data range.
(yaxs does the same thing for the y-axis).
You can use
plot(mtcars$mpg, ylim=c(0,50), yaxs="i")
I am surprised that there seems to be no question about this problem. At least I haven't found any with an accurate answer.
Suppose the easy case of rolling two dices and adding the pips shown. Possible results range from 2 to 12. Now I want to plot the histogram for this event, i.e. one bin per possible number. That would make 11 bins (2,3,4,5...12)
# Example dataset: how often did we get "2","3", "4"(1x2, 3x3, 2x4, 4x5, 8x6, 14x7, ...)
Dice <- c(2,rep(3,3),rep(4,2),rep(5,4),rep(6,8),rep(7,14),rep(8,9),rep(9,5),rep(10,4),rep(11,1),rep(12,2))
hist(Dice,breaks=seq(2,12)) # custom breaks return 10 bins (9 breaks)
hist(Dice,breaks=11) # same for automatic breaks (and for breaks=12 or 13...)
What I need is a histogram plot with 11 bins - that is one bin per possible result. How can I trick R into doing this?
Thank you!
hist(Dice,breaks=seq(1.5,12.5))
This is not an histogram per se, but you could try this:
barplot(table(Dice))
This question already has answers here:
Histogram with Logarithmic Scale and custom breaks
(7 answers)
Closed 10 years ago.
So I have a vector of integers, quotes, which I wish to see whether it observes a power law distribution by plotting the frequency of data points and making both the x and y axes logarithmic. However, I am not quite sure how to accomplish this in R. I can currently create a histogram using
hist(quotes, breaks = max(quotes))
But the axes are all linear.
There's probably a better way to do this, but this (basically) works:
data = rnorm(1000,0,1)
r <- hist(log(data))
plot(r$breaks[-1],log(r$counts))
EDIT: Better solution:
r <- hist(data)
plot(r$breaks[-1], r$counts, log='xy', type='h')
# or alternatively:
barplot(r$counts, log="y", col="white", names.arg=r$breaks[-1])
The barplot version doesn't have a transformed x axis for reasons that will become clear if you try it with the x axis transformed.