我试图在R中重现我多年前在SAS中做过的图表,结果由两个密度图和底部的地毯线组成,显示了观察结果。像这样:
但我无法弄清楚如何将两次调用的结果偏移到rug()
。我想这样做是为了使两个系列在视觉上更加鲜明,特别是如果用黑白打印。
以下是数据:
# North America
sub1 <-
c(1666, 1798, 1872, 1872, 1872, 1873, 1874, 1877, 1884, 1905,
1910, 1910, 1913, 1913, 1913, 1914, 1914, 1915, 1916, 1917, 1925,
1919, 1920, 1923, 1925, 1926, 1929, 1928, 1928, 1931, 1937, 1939,
1944, 1944, 1957, 1957, 1962, 1965, 1966, 1965, 1969, 1969, 1971,
1971, 1971, 1972, 1973, 1973, 1974, 1974, 1974, 1974, 1975, 1975,
1975, 1975, 1976, 1977, 1977, 1978, 1978, 1979, 1981, 1981, 1981,
1982, 1982, 1983, 1985, 1985, 1987, 1988, 1989, 1990, 1990, 1990,
1990, 1991, 1991, 1993, 1992, 1994, 1999)
# Europe
sub2 <-
c(1530, 1533, 1545, 1550, 1556, 1562, 1569, 1570, 1572, 1581,
1605, 1603, 1603, 1614, 1617, 1624, 1623, 1626, 1632, 1637, 1644,
1646, 1654, 1654, 1657, 1663, 1662, 1669, 1671, 1686, 1686, 1687,
1693, 1693, 1701, 1710, 1711, 1712, 1724, 1727, 1745, 1741, 1748,
1752, 1752, 1752, 1753, 1765, 1760, 1763, 1765, 1765, 1781, 1776,
1778, 1779, 1782, 1782, 1782, 1785, 1786, 1787, 1794, 1795, 1796,
1798, 1800, 1800, 1801, 1801, 1809, 1811, 1817, 1819, 1825, 1821,
1822, 1825, 1827, 1828, 1832, 1830, 1832, 1833, 1833, 1833, 1833,
1836, 1836, 1837, 1838, 1839, 1839, 1843, 1843, 1843, 1844, 1846,
1846, 1851, 1852, 1853, 1855, 1857, 1857, 1857, 1861, 1861, 1863,
1868, 1869, 1869, 1869, 1872, 1874, 1874, 1874, 1875, 1875, 1877,
1877, 1878, 1878, 1879, 1879, 1889, 1880, 1882, 1882, 1883, 1884,
1884, 1884, 1884, 1885, 1885, 1885, 1888, 1889, 1892, 1895, 1896,
1899, 1901, 1904, 1911, 1912, 1913, 1920, 1923, 1924, 1929, 1930,
1933, 1967, 1969, 1975, 1983, 1988, 1989, 1990, 1996)
这是我在R中使用的代码:
d1 <- density(sub1, from=1500, to=1990, bw="sj", adjust=2.5) # adjust=2.5 to more nearly equate
d2 <- density(sub2, from=1500, to=1990, bw="sj", adjust=0.75) # adjust=0.75
f1 <- d1$y
f2 <- d2$y
ref <-c(1600, 1700, 1800, 1850, 1900, 1950, 1975)
abline(v= ref, lty=2, lwd=1, col="blue")
labx<-c(1550, 1650, 1750, 1825, 1875, 1925, 1962, 1987)
laby<- 0.003 + 0.0003 * c(0, 1, 2, 3, 6, 3, 5, 2)
txt1 <-c("Early maps\n& diagrams",
"Measurement\n& theory",
"New graphic\nforms",
"Modern\nage",
"Golden Age",
"Modern dark\nages",
"Re-birth",
"Hi-D Vis")
xlim <- range(c(d1$x, d2$x), na.rm=TRUE)
ylim <- range(0, f1, f2)
plot(d1, xlim=xlim, ylim=ylim, col="red", lwd=2,
main="Milestones: Place of development",
xlab="Year", ylab="Relative frequency")
lines(d2, xlim=xlim, ylim=ylim, lwd=2)
abline(v= ref, lty=3, col="blue")
laby<- 0.008 + 0.0008 * c(0, 1, 2, 3, 5, 3, 5, 2)
text(labx, laby, labels=txt1, cex=1.2, xpd=TRUE)
# how to offset the two sets of rug lines?
rug(sub1, quiet=TRUE, col="red")
rug(sub2, quiet=TRUE)
答案 0 :(得分:2)
来自?rug
:
...: further arguments, passed to 'axis', such as 'line' or 'pos' for specifying the location of the rug.
然后,从?axis
开始,请参阅
line: the number of lines into the margin at which the axis line will be drawn, if not 'NA'. pos: the coordinate at which the axis line is to be drawn: if not 'NA' this overrides the value of 'line'.
我会在这里使用line
。只需用以下代码替换最后两行:
rug(sub1, quiet=TRUE, col="red", line = -1)
rug(sub2, quiet=TRUE)
您可能还想调整ylim
,以便红地毯不会侵犯线条。