Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I plot the following in R?

Tags:

plot

r

I'm new to plotting in R so I ask for your help. Say I have the following matrix.

mat1 <- matrix(seq(1:6), 3)
dimnames(mat1)[[2]] <- c("x", "y")
dimnames(mat1)[[1]] <- c("a", "b", "c")
mat1
  x y
a 1 4
b 2 5
c 3 6

I want to plot this, where the x-axis contains each rowname (a, b, c) and the y-axis is the value of each rowname (a = 1 and 4, b = 2 and 5, c = 3 and 6). Any help would be appreciated!

|     o
|   o x
| o x
| x
|_______
  a b c
like image 928
Jay Avatar asked Nov 26 '12 21:11

Jay


People also ask

How do you plot a graph in R studio?

First, download the file and load it into your favorite text editor. Replace ss+ with t to create two tab delimited columns. I think this is probably easier than trying to get R to read data separated by at least 2 spaces, as the source file seems to be. Now, load your data into R.

Can you plot a list in R?

Two-dimensional list can be created in R programming by creating more lists in a list or simply, we can say nested lists. The list() function in R programming is used to create a list. In this article, we'll learn to create plot graph using Two Dimensional List in R programming.


1 Answers

Here's one way using base graphics:

plot(c(1,3),range(mat1),type = "n",xaxt ="n")
points(1:3,mat1[,2])
points(1:3,mat1[,1],pch = "x")
axis(1,at = 1:3,labels = rownames(mat1))

enter image description here

Edited to include different plotting symbol

like image 146
joran Avatar answered Oct 20 '22 07:10

joran