The dependence of the ratio between the B0s and B+ hadron production fractions, fs/fu, on the transverse momentum (pT) and rapidity of the B mesons is studied using the decay channels B0s→J/ψϕ and B+→J/ψK+. The analysis uses a data sample of proton-proton collisions at a center-of-mass energy of 13 TeV, collected by the CMS experiment in 2018 and corresponding to an integrated luminosity of 61.6 fb−1. The fs/fu ratio is observed to depend on the B pT and to be consistent with becoming asymptotically constant at large pT. No rapidity dependence is observed. The ratio of the B0 to B+ meson production fractions, fd/fu, is also measured, for the first time in proton-proton collisions, using the B0→J/ψK*0 decay channel. The result is found to be within 1 standard deviation of unity and independent of pT and rapidity, as expected from isospin invariance.