Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: making tick marks at specified intervals on x-axis when number are large [duplicate]

I know this seems trivial, but for some reason, I cannot figure out how to do this.

I have made this plot

plot

using these commands:

data1 <- read.table("data_for_question.txt", sep="\t", header=TRUE)
plot(data1, type="l", lwd=2)

from data I will add below.

It's a nice enough plot but the tick marks and numbers are too far apart. I would like to have a tick mark at every 4th data point, which would be in increments of 20,000 time points, rather than 50,00 as shown here.

I apologize for the long list of data, but the number of data points being graphed appears to be relevant to my issues in tick marking.

Thanks!

I know this seems like a duplicate, but I have checked, for hours, other questions like it and none of the solutions worked for me for some reason.

data:

Time    Counts
0   29
5000    68
10000   55
15000   34
20000   61
25000   47
30000   42
35000   39
40000   25
45000   3
50000   30
55000   2
60000   36
65000   29
70000   13
75000   13
80000   17
85000   11
90000   10
95000   29
100000  20
105000  8
110000  0
115000  1
120000  1
125000  1
130000  1
135000  0
140000  6
145000  0
150000  0
155000  0
160000  0
165000  0
170000  0
175000  0
180000  4
185000  0
190000  0
195000  0
200000  0
205000  0
210000  0
215000  0
220000  0
225000  0
230000  0
235000  0
240000  0
245000  0
250000  0
255000  0
260000  87
265000  0
270000  0
275000  0
280000  0
285000  0
290000  0
295000  0
300000  0
305000  0
310000  0
315000  1
320000  1
325000  0
330000  0
335000  1
340000  0
345000  0
350000  0
355000  2
360000  0
365000  0
370000  1
375000  0
380000  0
385000  0
390000  0
395000  0
400000  0
405000  2
410000  0
415000  3
420000  0
425000  0
430000  0
435000  0
440000  0
445000  0
450000  1
455000  0
460000  0
465000  2
470000  0
475000  1
480000  3
485000  0
490000  0
495000  1
500000  0
505000  0
510000  0
515000  1
520000  0
525000  0
530000  0
535000  0
540000  0
545000  0
550000  0
555000  0
560000  0
565000  0
570000  0
575000  0
580000  0
585000  0
590000  0
595000  0
600000  0
605000  0
610000  1
615000  11
620000  1
625000  2
630000  3
635000  0
640000  3
645000  2
650000  6
655000  7
660000  1
665000  5
670000  1
675000  1
680000  2
685000  0
690000  0
695000  3
700000  0
705000  0
710000  0
715000  0
720000  0
725000  0
730000  0
735000  2
740000  0
745000  0
750000  2
755000  0
760000  0
765000  0
770000  0
775000  0
780000  0
785000  0
790000  2
795000  0
800000  0
805000  0
810000  0
815000  4
820000  0
825000  0
830000  1
835000  0
840000  0
845000  0
850000  0
855000  0
860000  0
865000  0
870000  0
875000  0
880000  0
885000  1
890000  0
895000  0
900000  0
905000  0
910000  0
915000  0
920000  0
925000  0
930000  1
935000  0
940000  2
945000  0
950000  1
955000  0
960000  0
965000  0
970000  11
975000  2
980000  0
985000  1
990000  0
995000  0
1000000 0
1005000 4
1010000 0
1015000 2
1020000 0
1025000 0
1030000 2
1035000 0
1040000 0
1045000 0
1050000 0
1055000 0
1060000 0
1065000 0
1070000 0
1075000 0
1080000 0
1085000 1
1090000 0
1095000 0
1100000 0
1105000 0
1110000 0
1115000 0
1120000 0
1125000 0
1130000 1
1135000 0
1140000 0
1145000 0
1150000 0
1155000 1
1160000 0
1165000 0
1170000 0
1175000 1
1180000 0
1185000 0
1190000 0
1195000 0
1200000 0
1205000 0
1210000 0
1215000 0
1220000 0
1225000 1
1230000 0
1235000 0
1240000 22
1245000 0
1250000 0
1255000 0
1260000 0
1265000 0
1270000 0
1275000 0
1280000 0
1285000 0
1290000 0
1295000 0
1300000 0
1305000 0
1310000 0
1315000 0
1320000 0
1325000 9
1330000 0
1335000 0
1340000 0
1345000 0
1350000 3
1355000 0
1360000 0
1365000 0
1370000 0
1375000 0
1380000 0
1385000 0
1390000 6
1395000 0
1400000 0
1405000 0
1410000 0
1415000 1
1420000 0
1425000 0
1430000 0
1435000 0
1440000 0
1445000 1
1450000 0
1455000 0
1460000 0
1465000 0
1470000 0
1475000 1
1480000 0
1485000 0
1490000 0
1495000 0
1500000 0
1505000 8
1510000 1
1515000 4
1520000 0
1525000 1
1530000 0
1535000 3
1540000 0
1545000 0
1550000 7
1555000 2
1560000 8
1565000 15
1570000 0
1575000 9
1580000 4
1585000 7
1590000 4
1595000 1
1600000 2
1605000 7
1610000 1
1615000 5
1620000 8
1625000 9
1630000 4
1635000 12
1640000 12
1645000 7
1650000 0
1655000 3
1660000 4
1665000 5
1670000 4
1675000 1
1680000 0
1685000 0
1690000 1
1695000 0
1700000 1
1705000 25
1710000 18
1715000 1
1720000 4
1725000 0
1730000 9
1735000 3
1740000 0
1745000 0
1750000 0
1755000 2
1760000 3
1765000 0
1770000 0
1775000 4
1780000 1
1785000 4
1790000 1
1795000 5
1800000 0
1805000 8
1810000 15
1815000 1
1820000 0
1825000 0
1830000 3
1835000 2
1840000 0
1845000 0
1850000 1
1855000 0
1860000 0
1865000 0
1870000 0
1875000 0
1880000 0
1885000 0
1890000 1
1895000 1
1900000 1
1905000 4
1910000 3
1915000 0
1920000 3
1925000 3
1930000 1
1935000 0
1940000 1
1945000 3
1950000 5
1955000 8
1960000 4
1965000 5
1970000 26
1975000 4
1980000 11
1985000 9
1990000 6
1995000 1
2000000 2
2005000 1
2010000 0
2015000 1
2020000 4
2025000 2
2030000 5
2035000 8
2040000 2
2045000 0
2050000 0
2055000 5
2060000 2
2065000 3
2070000 10
2075000 8
2080000 0
2085000 4
2090000 0
2095000 8
2100000 2
2105000 7
2110000 4
2115000 0
2120000 30
2125000 3
2130000 0
2135000 8
2140000 5
2145000 2
2150000 10
2155000 9
2160000 8
2165000 1
2170000 0
2175000 4
2180000 0
2185000 3
2190000 0
2195000 1
2200000 0
2205000 0
2210000 0
2215000 1
2220000 0
2225000 0
2230000 0
2235000 0
2240000 0
2245000 0
2250000 0
2255000 0
2260000 0
2265000 0
2270000 0
2275000 0
2280000 1
2285000 0
2290000 1
2295000 0
2300000 0
2305000 0
2310000 0
2315000 0
2320000 0
2325000 0
2330000 0
2335000 0
2340000 0
2345000 0
2350000 0
2355000 2
2360000 2
2365000 3
2370000 0
2375000 0
2380000 0
2385000 2
2390000 5
2395000 3
2400000 2
2405000 6
2410000 7
2415000 11
2420000 2
2425000 1
2430000 6
2435000 1
2440000 2
2445000 11
2450000 10
2455000 4
2460000 0
2465000 2
2470000 6
2475000 3
2480000 1
2485000 29
2490000 0
2495000 5
2500000 1
2505000 6
2510000 8
2515000 7
2520000 0
2525000 3
2530000 7
2535000 4
2540000 1
2545000 2
2550000 30
2555000 1
2560000 2
2565000 12
2570000 4
2575000 4
2580000 9
2585000 0
2590000 1
2595000 1
2600000 3
2605000 0
2610000 9
like image 465
user1669785 Avatar asked Dec 12 '22 11:12

user1669785


1 Answers

# plot with suppressed x axis
# xaxt = "n"  (see ?par)
plot(Counts ~ Time, data = df, type = "l", xaxt = "n")

# add a new x axis, using 'axis' function
#  with own specifications
# argument 'at': "the points at which tick-marks are to be drawn"
# make a sequence of numbers where the tick-marks is to be places using 'seq'
# from 0 to max Time with increments ('by') of 200 000. 
at <- seq(from = 0, to = max(df$Time), by = 200000)

# argument 'side': specifies which side of the plot the axis is to be drawn on.
# 1 = x axis

# argument 'labels'
# "If labels is not specified, the numeric values supplied or calculated for 'at'
# are converted to character strings as if they were a numeric vector printed
# but note: "The code tries hard not to draw overlapping tick labels,
# and so will omit labels where they would abut or overlap previously drawn labels"

axis(side = 1, at = at)

# to make the labels fit, one can use the 'las' argument
# "numeric in {0,1,2,3}; the style of axis labels"
# where 2: always perpendicular to the axis

plot(Counts ~ Time, data = df, type = "l", xaxt = "n", xlab = "")
axis(side = 1, at = at, las = 2, hadj = 0.9)
mtext(text = "Time", side = 1, line = 4)
like image 117
Henrik Avatar answered Dec 14 '22 00:12

Henrik