0

I have a text file that I converted into a numeric vector:

numbers <- scan("list_of_numbers.txt")

I then put it into a table:

t <- table(numbers)

Which outputs like this:

  1      2      3      4      5      6      7      8      9     10     11 
621266 496647 436229 394595 353249 305882 253983 199455 147380 102872  67255 
12     13     14     15     16     17     18     19     20     21     22 
41934  24506  13778  7179   3646   1778   816    436    217    114    74 
23     24     25     26     27     28     29     30     31     32     33 
49     44     26     21     19     21     20     14      9     17     14 
34     35     36     37     38     39     40     41     42     43     44 
 7     11      9     14      3      5      8      4      4      2      3 
45     46     47     55     56     60     62     63     69     70     72 
 2      1      2      2      2      1      1      1      3      2      1 
78     82     85     93     95     114    125    265    331    350 
 1      1      1      1      1      1      1      1      1      1 

How would I plot a line graph with x axis of numbers 1 - 25 and y axis the frequency values of the x axis all in the terminal window?

In addition, how can a plot like this (which is default saved as a .pdf file) be viewd in the linux terminal?

Most commands like less, cat, and xdg-open output a bunch of strange unreadable symbols. I am working over in an ssh server.

1 Answer 1

0

Do you want an ASCII plot (i.e. it will show up in a terminal)? A plot over X11? The output device is important (as R can plot to many different devices).

If you are forwarding X11 via ssh and have R in an interactive session, then:

plot(t[1:24], type = "l")

should get you a basic line plot in a new X11 window. Do a ?plot in R for more options on tweaking the format.

You can deliberately try to plot it to X11 if that doesn't work with:

x11(width = 8, height = 8)
plot(t[1:24], type = "l")
dev.off()

If you don't have X11 up or forwarding, you can get a basic points plot in-ASCII terminal with the txtplot function from the txtplot package.

Similarly, there is a PDF device in R as well that works the same was as the X11 example above, but will generate output to a file:

pdf(file = "filenameyoudesire.pdf", width = 8, height = 8)
plot(t[1:24], type = "l")
dev.off()

There are other methods of plotting and formatting plots, but this should get you started.

Also, despite R being intelligent enough to understand the difference, t is also a built-in function name (do a ?t in R to see what it does) so you should really avoid using it for a variable name.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .