[R] 2D and 3D graphing

EK Esawi eke@@w| @end|ng |rom y@hoo@com
Fri Mar 27 01:48:15 CET 2020


 
Thank you all; Bert, LMH, Duncan and others. I think i have a good idea now to resolve the problem i have.
Wish you all the best and and hope you're all safe
EK
    On Thursday, March 26, 2020, 5:24:03 PM EDT, LMH <lmh_users-groups using molconn.com> wrote:  
 
 Bert Gunter wrote:
> " I have attached a .zip with some sample data and a list of R
> terminal commands..."
>
> Maybe to Ek, but not to the list. The server strips most attachments.
>
> Bert Gunter

Well that is annoying.

I have included the data below. There are 4 files.

If you start under the line below with the file name and copy, you should be
able to past into individual files. This is one reason I often prefer forums
to mailing lists.

LMH


file: point-set_5,57,55,41,sum.txt (tab delimited)
id    class    x    y    z
5    1    0    0    0
57    2    0.182546    -0.47885    0.35814
55    2    1.96957    2.39455    -0.523553
41    2    -3.79805    -1.21401    0.167462
sum    3    -5.84668125    4.57091875    -1.13686875


file: connections_5,57,55,41,sum_all.txt (tab delimited)
x0    x1    y0    y1    z0    z1
0    0.182546    0    -0.47885    0    0.35814
0    1.96957    0    2.39455    0    -0.523553
0    -3.79805    0    -1.21401    0    0.167462
0    -5.84668125    0    4.57091875    0    -1.13686875
0.182546    1.96957    -0.47885    2.39455    0.35814    -0.523553
0.182546    -3.79805    -0.47885    -1.21401    0.35814    0.167462
0.182546    -5.84668125    -0.47885    4.57091875    0.35814    -1.13686875
1.96957    -3.79805    2.39455    -1.21401    -0.523553    0.167462
1.96957    -5.84668125    2.39455    4.57091875    -0.523553    -1.13686875
-3.79805    -5.84668125    -1.21401    4.57091875    0.167462    -1.13686875


file: R_terminal_input.txt
# this is the list of R terminal input to creat the 3D plot
# all of the inputs can be made at once by select all | copy
# and then paste in the terminal

# this implies that you have created a directory containing the files
# point-set_5,57,55,41,sum.txt
# point-set_5,57,55,41,sum_all_connect.txt
# and set that as your working directory in setwd() below

# load rgl library
library(rgl)

# set working directory
setwd("C:/rgl_test")

# remove any existing objects
rm(list=ls())

# read in the table of points for plot
points <- read.table("point-set_5,57,55,41,sum.txt", header = TRUE)

# read in the values for the points that will be connected
connect <- read.table("connections_5,57,55,41,sum_all.txt", header = TRUE)

# create the "class" category
class <- as.factor(points[,2])

# create the plot of colums x,y,z
# the color of the points is indicated by the "class" column value
# change the boolean of "box" or "axes" to remove that feature from the plot
plot3d(points$x, points$y, points$z, col=class, size=7, box = TRUE, axes = TRUE)

# add the connections
segments3d(x=t(connect[,c(1,2)]), y=t(connect[,c(3,4)]), z=t(connect[,c(5,6)]))

# add text labels to the plot
# you can skip this if you don't want labels
# this adds value of the id column read into the points object
text3d(points$x, points$y, points$z, text=points$id, adj = c(1, 1), cex=1)

# add the pan function
# right click will now all the user to pan across the plot

pan3d <- function(button) {
start <- list()

begin <- function(x, y) {
 start$userMatrix <<- par3d("userMatrix")
  start$viewport <<- par3d("viewport")
  start$scale <<- par3d("scale")
  start$projection <<- rgl.projection()
  start$pos <<- rgl.window2user( x/start$viewport[3], 1 - y/start$viewport[4], 0.5,
                              projection=start$projection)
}

update <- function(x, y) {
 xlat <- (rgl.window2user( x/start$viewport[3], 1 - y/start$viewport[4], 0.5,
                        projection = start$projection) - start$pos)*start$scale
mouseMatrix <- translationMatrix(xlat[1], xlat[2], xlat[3])
par3d(userMatrix = start$userMatrix %*% t(mouseMatrix) )
}
rgl.setMouseCallbacks(button, begin, update)
cat("Callbacks set on button", button, "of rgl device",rgl.cur(),"\n")
}
pan3d(2)


file: _make_connect.sh
#!/bin/bash

# input file is first argument
input_file=$1
# which connections to output, all, vec, tet
output_type=$2
# create output file name
output_file=$(echo $input_file | sed 's/.txt//1')
# add output type
output_file=$output_file'_'$output_type'_connect.txt'

# make sure the output type is supported
if [ "$output_type" != "all" ] && [ "$output_type"  != "vec" ] && [ "$output_type"  != "tet" ]; then
  echo ""
  echo "output types are,"
  echo ""
  echo "all: connect all points"
  echo "vec: show vectors, connect point B to A,D,E,C"
  echo "tet: show tetrahedron, connect A to D,E,C, D to E,C, E to C"
  echo ""
  echo "use second argument to specify output type"
  echo ""
  exit 1
fi

# storage for row data
declare -a header
declare -a B_row
declare -a A_row
declare -a D_row
declare -a E_row
declare -a C_row
# to know what row is being read
row_counter=0

# loop through tabe delimited input file and store each row
while IFS=$'\t' read -a temp_row
do

  # for the header row
  if [ $row_counter == 0 ]; then
      header=("${temp_row[@]}")
      (( row_counter++ ))
  # for the B_row
  elif [ $row_counter == 1 ]; then
      B_row=("${temp_row[@]}")
      (( row_counter++ ))
  # for the A_row
  elif [ $row_counter == 2 ]; then
      A_row=("${temp_row[@]}")
      (( row_counter++ ))
  # for the D_row
  elif [ $row_counter == 3 ]; then
      D_row=("${temp_row[@]}")
      (( row_counter++ ))
  # for the E_row
  elif [ $row_counter == 4 ]; then
      E_row=("${temp_row[@]}")
      (( row_counter++ ))
  # for the C_row
  elif [ $row_counter == 5 ]; then
      C_row=("${temp_row[@]}")
  # check the max rows
  elif [ $row_counter == 6 ]; then
      echo "too many rows in input file, there should only be 5"
  fi

done < $input_file

# write output file
# header row
echo -e "x0"'\t'"x1"'\t'"y0"'\t'"y1"'\t'"z0"'\t'"z1" > $output_file

if [ "$output_type" == "all" ] || [ "$output_type"  == "vec" ] ; then
  # connect point B to each other point
  echo -e "${B_row[2]}"'\t'"${A_row[2]}"'\t'"${B_row[3]}"'\t'"${A_row[3]}"'\t'"${B_row[4]}"'\t'"${A_row[4]}" >>
$output_file
  echo -e "${B_row[2]}"'\t'"${D_row[2]}"'\t'"${B_row[3]}"'\t'"${D_row[3]}"'\t'"${B_row[4]}"'\t'"${D_row[4]}" >>
$output_file
  echo -e "${B_row[2]}"'\t'"${E_row[2]}"'\t'"${B_row[3]}"'\t'"${E_row[3]}"'\t'"${B_row[4]}"'\t'"${E_row[4]}" >>
$output_file
  echo -e "${B_row[2]}"'\t'"${C_row[2]}"'\t'"${B_row[3]}"'\t'"${C_row[3]}"'\t'"${B_row[4]}"'\t'"${C_row[4]}" >>
$output_file
fi

if [ "$output_type" == "all" ] || [ "$output_type"  == "tet" ] ; then
  # connect point A to each other point except A,B
  echo -e "${A_row[2]}"'\t'"${D_row[2]}"'\t'"${A_row[3]}"'\t'"${D_row[3]}"'\t'"${A_row[4]}"'\t'"${D_row[4]}" >>
$output_file
  echo -e "${A_row[2]}"'\t'"${E_row[2]}"'\t'"${A_row[3]}"'\t'"${E_row[3]}"'\t'"${A_row[4]}"'\t'"${E_row[4]}" >>
$output_file
  echo -e "${A_row[2]}"'\t'"${C_row[2]}"'\t'"${A_row[3]}"'\t'"${C_row[3]}"'\t'"${A_row[4]}"'\t'"${C_row[4]}" >>
$output_file
  # connect point D to point C,E
  echo -e "${D_row[2]}"'\t'"${E_row[2]}"'\t'"${D_row[3]}"'\t'"${E_row[3]}"'\t'"${D_row[4]}"'\t'"${E_row[4]}" >>
$output_file
  echo -e "${D_row[2]}"'\t'"${C_row[2]}"'\t'"${D_row[3]}"'\t'"${C_row[3]}"'\t'"${D_row[4]}"'\t'"${C_row[4]}" >>
$output_file
  # connect point E to point C
  echo -e "${E_row[2]}"'\t'"${C_row[2]}"'\t'"${E_row[3]}"'\t'"${C_row[3]}"'\t'"${E_row[4]}"'\t'"${C_row[4]}" >>
$output_file
fi









Bert Gunter wrote:
> " I have attached a .zip with some sample data and a list of R
> terminal commands..."
> 
> Maybe to Ek, but not to the list. The server strips most attachments.
> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> On Thu, Mar 26, 2020 at 10:04 AM LMH <lmh_users-groups using molconn.com> wrote:
>>
>> I have attached a .zip with some sample data and a list of R terminal commands. This
>> is not very interactive by does create a useful 3D plot. The data that I used was
>> generally not very large.
>>
>> In the sample commands "R_terminal_input.txt", the working directory is given as
>> "C:/rgl_test". Line 14 in the terminal commands sets the working directory location,
>> so edit that line if you are going to use a different location. If you just copy the
>> entire file with select all | copy and paste it into the R terminal you should get
>> the plot (as long as pwd is correct). Make sure to key "enter" after you paste or you
>> will not get the last line of code.
>>
>> You will see a plot of 5 points (5,57,55,41,sum) which are all connected by lines.
>> You can rotate the plot with left-click, pan the plot with right-click, and zoom with
>> the scroll wheel. The color of the data point is governed by the value of the "class"
>> column in the input file.
>>
>> The input data input is spreadsheet like and you should be able to add your own data
>> without too much difficulty.
>>
>> The format for the connecting lines is in the file
>> "connections_5,57,55,41,sum_all.txt" and is in the format,
>>
>> x0      x1      y0      y1      z0      z1
>>
>> Each line in the file is a vector where columns 0,2,4 are the xyz coordinates for the
>> tail of the vector and columns 1,3,5 are the xyz coordinates for the tip.
>>
>> I have included a little script "_make_connect.sh". I wrote this to generate the
>> connection rows from the input file. The first argument to the script is the input
>> file name. The second argument specifies one of three types of connection sets.
>>
>> all -> all points are connected
>> vec -> creates a vector from the first row to each other row
>> tet -> creates in irregular tetrahedron connection all points except the first row
>>
>> This was all created for something I was working on and I don't know how directly it
>> will apply to what you need.
>>
>> Feel free to ask questions. It is probably easier to do that than for me to
>> anticipate all possible areas of confusion.
>>
>> LMH
>>
>>
>>
>> EK Esawi wrote:
>>>
>>> Thank you Ista and LMH.
>>> I want to plot 3D scatter with options to connect the surface with lines;that's just like points connected with a  line in 2D and i may want to fit a surface to the data for soem of teh plots. LMH pointed out to what concerned me and the reason for the question which was i don't want to spend time in a package and then find out that t doesn't do what i want to.
>>> Thanks again and best of luckEK
>>>    On Monday, March 23, 2020, 6:02:33 PM EDT, LMH <lmh_users-groups using molconn.com> wrote:
>>>
>>>  Hello,
>>>
>>> I use rgl for 3D plots.
>>>
>>> Mostly I use this for a 3D scatter plot where I can zoom and pan for closer analyses
>>> of the data. I also can manually connect any points with lines. It took me a bit to
>>> get the options right so the plot appears the way I want it to. In my opinion, there
>>> are a few unresolved issues with the units and labels on the axis, but most of the
>>> data I looked at was in principle components so x, y, and z were in the same units.
>>>
>>> I can post a sample of the terminal input that I used if that would help.
>>>
>>> LMH
>>>
>>>
>>> Ek Esawi wrote:
>>>> Hi All--
>>>>
>>>> I have been looking into 2D and 3D graphing packages. Based on what i
>>>> read, it seems that ggplot2 is the best and I like it too, but ggplot2
>>>> doesn't have 3D plotting capabilities. I read that other packages
>>>> (plot_ly, rgl, rayshader) can be used with ggplot2 to create 3D
>>>> charts, but not sure if that’s the way to go.
>>>>
>>>> The question is: what is/are the best 2D and 3D graphing packages? I
>>>> read that lattice, and a few others, has “limited” 3D charting
>>>> capabilities.
>>>>
>>>> Thanks in advance--EK
>>>>
>>>> ______________________________________________
>>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>> ______________________________________________
>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 

  
	[[alternative HTML version deleted]]



More information about the R-help mailing list