Sean
Sean

Reputation: 1313

Plotting in R in Linux Terminal

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.

Upvotes: 4

Views: 5639

Answers (2)

Lerner Zhang
Lerner Zhang

Reputation: 7150

I think it's very convenient to use txtplot::txtplot as follow:

> cat("1 2 3 4 5 6", file = "list_of_numbers.txt", sep = "\n")
> numbers <- scan("list_of_numbers.txt")
Read 6 items
> t <- table(numbers)
> txtplot(t)

enter image description here

You can install it just by this command:

install.packages('txtplot') 

I found that Jupyter may be the best wheel for us to handle that, and we can equip that following this tutorial: Embed Graphs In Jupyter Notebooks in R

References:

  1. How To Install R on Ubuntu 18.04
  2. scan

Upvotes: 1

alko989
alko989

Reputation: 7938

You can use fbi, the linux framebuffer imageviewer to open pdf files in the linux console.

A small problem can be that it needs root privileges. It seems like it can not run through R using system, it complains about not being a linux console. But you can use it in the terminal like:

sudo fbi Rplots.pdf

As for the plotting part of your question you can just use something like:

plot(t, xlim = c(1, 25))

Hope it helps,

alex

Upvotes: 2

Related Questions