Home > Net >  Why don't my base plot type="b" produce a line and symbols?
Why don't my base plot type="b" produce a line and symbols?

Time:06-07

I have now spent too much time trying to make my very simple script produce a base plot with a line and symbols, but to me it only produces the following plot with horisontal line segments, which it shouldn't. This should be very simple, but something is wrong.

enter image description here

My script:

 plot(sumtab$Year,sumtab$Count,type='b',pch=19,xlab="",ylab="",col='black',cex.lab=1.5,las=1)

My dataframe:

dput(sumtab)
structure(list(Year = structure(1:43, .Label = c("1938", "1939", 
"1940", "1941", "1942", "1943", "1944", "1945", "1946", "1947", 
"1948", "1949", "1950", "1951", "1952", "1953", "1954", "1955", 
"1956", "1957", "1958", "1959", "1960", "1961", "1962", "1963", 
"1964", "1965", "1966", "1967", "1968", "1969", "1970", "1971", 
"1972", "1973", "1974", "1975", "1977", "1978", "1979", "1980", 
"1981"), class = "factor"), Count = c(49L, 23L, 7L, 14L, 57L, 
13L, 28L, 12L, 32L, 29L, 43L, 34L, 12L, 24L, 13L, 9L, 13L, 26L, 
40L, 48L, 39L, 69L, 82L, 112L, 124L, 90L, 77L, 104L, 162L, 36L, 
86L, 231L, 246L, 57L, 28L, 1L, 6L, 2L, 7L, 64L, 221L, 52L, 13L
)), class = "data.frame", row.names = c(NA, -43L))

CodePudding user response:

If you'd rather not change the data you can transform your x variable into numeric during plotting

plot(as.numeric(as.character(sumtab$Year)),sumtab$Count,type='b')

CodePudding user response:

You don't have to specify the x-variable, R will take its indices:

plot(sumtab$Count,
     type='b',
     pch=19,
     xaxt = "n",
     xlab="", ylab="", col='black', cex.lab=1.5, las=1)
axis(1, at = 1:nrow(sumtab), labels = sumtab$Year, las = 2)

enter image description here

  • Related