Skip to content

plot.window arguments being ignored?

3 messages · ARRRRR_user, Viknesh, Bryan Hanson

#
Hi,
Why, when I run the script below, is my y-axis range command being ignored? 
I.e., the y axis graphed consistently fits the line specified in the plot
command, but never fits the line I'm trying to add in the subsequent line
command.
This is my first R script, so if I'm doing anything else wacky that jumps
out, please let me know.  I'm an advanced Stata user, but R is new to me.
The script is designed to simulate a binary-choice decision field theory
example with internally-controlled stop time, as per Neural Networks 19
(2006) 1047-1058.
Thanks in advance!!
-Dan


#-----------------
# Starting preference state; two choices
preference<-c(0,0)
# Preference threshold; when one of the preference states exceeds this
threshhold, deliberation stops
theta<- 2
# Contrast matrix
contrast<-array(c(1, -1, -1, 1), dim=c(2,2))
# Value Matrix; three possible states of the world
value<-array(c(1,0, 0, 1, .5,.5), dim=c(2, 3))
# Feedback Matrix
feedback<-array(c(1, .5, .5, 1), dim=c(2,2))

# Time
t<- array(0, dim=c(1,1))

# Arrays to keep track of history for graphing
time_for_graph<-array(t, dim=c(1,1))
preference_for_graph<-array(preference, dim=c(1,2))

# Moving through time until preference crosses the threshold theta
while (preference[1]<theta && preference[2]<theta) {  
	
	#stochastic weights for this time period
	weight<-rnorm(3,0,1)
	
	#updating preference
	preference<-feedback%*%preference + contrast%*%value%*%weight

	#updating history
	t<-t+1
	time_for_graph<-rbind(time_for_graph, t)
	preference_for_graph<-rbind(preference_for_graph, array(preference,
dim=c(1,2)))

	#updating graph ranges
	ry<-range(preference_for_graph)
	rx<-range(time_for_graph)

	#updating graph	
	plot(time_for_graph[,1], preference_for_graph[,1], type="b",
plot.window(rx, ry), xlab="Time", ylab="Preference")
	lines(time_for_graph[,1], preference_for_graph[,2], type="b", col="red",
ylim=ry)
}
#-------------
#
Hi Dan,

I think it's because lines() is meant for you to add lines to a plot. Hence
it does not make R redraw the plot. If you wish to set the limits of the
y-axis (or x-axis for that matter), you should do it in the plot function
call, and then call lines() without the "ylim=ry" part. So your final two
lines could look like this:
  
  plot(time_for_graph[,1], preference_for_graph[,1], type="b", xlim=rx,
ylim= ry, xlab="Time", ylab="Preference") 
  lines(time_for_graph[,1], preference_for_graph[,2], type="b", col="red")

HTH,
Vik
---
Grad Student
Department of Statistics
University of Florida
ARRRRR_user wrote:

  
    
#
Dan, with base graphics, the> plot command determines the ylim, >lines can
only add lines/points to an exisiting graph, it cannot modify the existing
ylim.

You have two choices.  1) Before proceeding to the graph, determine which of
the two data sets has the greater range and call >plot with that range, then
the subsequent >lines command will "fit".  2) use a dlfferent graphics
system such as lattice or ggplot2.  In these packages, the entire graph is
computed in the background, then plotted explicitly.  So as you build up the
graph by adding things to it, the routines automatically resize as
necessary.  Both lattice and ggplot2 have books and very nice web sites
where you can find an example like yours.

Bryan
*************
Bryan Hanson
Acting Chair
Professor of Chemistry & Biochemistry
DePauw University, Greencastle IN USA
On 11/7/09 6:28 PM, "ARRRRR_user" <dweitzenfeld at gmail.com> wrote: