hmm
hmm

Reputation: 3

Ploting histograms using stats library

I'm very new to programming, so I'm sorry if this is a basic question that has been answered a bunch of times. I'm trying to plot a histogram that has months on the X-axis and number of sunspots on the Y-axis. To get the data I'm using stats library.

x <- datasets::sunspot.month
h <- hist(x, breaks=12, col="red", xlab="Month", main="Histogram with Normal Curve")

this is the code that i currently have and I'm getting some weird (probably wrong results), any advice on what I should try?

P.S. You can ignore the nominal curve I'll try to do that on my own once I get this right.

Results of the plot

How the data looks like

Upvotes: 0

Views: 153

Answers (1)

r2evans
r2evans

Reputation: 160782

Because it's a time-series, we can extract the "time" component with the (wait for it) time function :-)

time(sunspot.month)
#       Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
# 1749 1749 1749 1749 1749 1749 1749 1750 1750 1750 1750 1750 1750
# 1750 1750 1750 1750 1750 1750 1750 1750 1751 1751 1751 1751 1751
# 1751 1751 1751 1751 1751 1751 1751 1752 1752 1752 1752 1752 1752
# 1752 1752 1752 1752 1752 1752 1752 1752 1753 1753 1753 1753 1753
# ...

If you look at the data a little more closely, the "time" values are decimal years,

options(digits=9)
head(time(sunspot.month))
# [1] 1749.00000 1749.08333 1749.16667 1749.25000 1749.33333 1749.41667

We can extract the "month only" from this by using a modulus of 1 (%% 1), which will give us a 0-based array. From there, we can table it to get the counts of each month, and then @user438383 suggested, present a bar plot.

Also, for labeling the months, we can use the base::month.abb constant.

barplot(table(as.integer(1+12*(time(sunspot.month) %% 1))),
        names.arg=month.abb)

sunspots bar plot, by month

Upvotes: 1

Related Questions