This function provides a simulation to the process of flipping coins and
computes the frequencies for heads and tails.
If faces is a single integer, say 2, a sequence of integers from 1 to
faces will be used to denote the faces of a coin; otherwise this
character vector just gives the names of each face.
When the \(i\)-th face shows up, a colored thin rectangle will be added to
the corresponding place (the \(i\)-th bar), and there will be corresponding
annotations for the number of tosses and frequencies.
The special argument grid is for consideration of a too large number
of flipping, in which case if you still draw horizontal lines in these
rectangles, the rectangles will be completely covered by these lines, thus we
should specify it as NA.
At last the frequency for each face will be computed and shown in the header
of the plot – this shall be close to prob if ani.options('nmax') is large enough.
This animation has provided a simulation of flipping coins which might be helpful in understanding the concept of probability.
library(animation)
ani.options(interval = 0.2, nmax = 100)
## a coin would stand on the table?? just kidding :)
flip.coin(faces = c("Head", "Stand", "Tail"), type = "n", prob = c(0.45, 0.1, 0.45),
col = c(1, 2, 4))
flip.coin(bg = "yellow")
Donate
As a freelancer (currently working as a contractor) and a dad of three kids, I truly appreciate your donation to support my writing and open-source software development! Your contribution helps me cope with financial uncertainty better, so I can spend more time on producing high-quality content and software. You can make a donation through methods below.
-
Venmo:
@yihui_xie, or Zelle:xie@yihui.name -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
xie@yihui.name. Please choose the payment type as “Family and Friends” (instead of “Goods and Services”) to avoid extra fees. -
If you don’t have Paypal, you may donate through this link via your debit or credit card. Paypal will charge a fee on my side.
-
-
Other ways:
WeChat Pay (微信支付:谢益辉) Alipay (支付宝:谢益辉) 

When sending money, please be sure to add a note “gift” or “donation” if possible, so it won’t be treated as my taxable income but a genuine gift. Needless to say, donation is completely voluntary and I appreciate any amount you can give.
Please feel free to email me if you prefer a different way to give. Thank you very much!
I’ll give back a significant portion of the donations to the open-source community and charities. For the record, I received about $30,000 in total (before tax) in 2024-25, and gave back about $15,000 (after tax).