2017-10-25 11 views
0

私はSASで前にグラフを再現しようとしています。その結果は、2つの密度プロットとラグラインが観測結果を示しています。これと同じように:R:2セットのrug()ラインの垂直オフセット

enter image description here

しかし、私はrug()への二つの呼び出しの結果を相殺する方法を見つけ出すことはできません。私は2つのシリーズをより視覚的に区別するために、特にb/wで印刷する場合にこれを行いたいと思います。ここで

はデータである。ここでは

# 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) 

答えて

2

?rugから:

その後
...: further arguments, passed to 'axis', such as 'line' or 'pos' 
    for specifying the location of the rug. 

?axisから、私はここにlineを使用します

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'. 

を参照してください。あなたはまた、赤いじゅうたんがラインを侵害しないように、あなたのylimを調整したい場合があります

rug(sub1, quiet=TRUE, col="red", line = -1) 
rug(sub2, quiet=TRUE) 

:ちょうどであなたの最後の2行を交換してください。

関連する問題