[R] Fwd: only plot borders of a region in a scatter plot
Jim Lemon
drjimlemon at gmail.com
Fri Aug 5 05:01:48 CEST 2016
Hi Zun Yin,
A slight improvement follows, which tries to "inflate" the pixellation
locally. This, in addition to the offset, might do it for you.
pixel8<-function(x,y) {
nsteps<-length(x)-1
newx<-x[1]
newy<-y[1]
lastdx<-lastdy<-0
for(i in 1:nsteps) {
dx<-diff(x[i:(i+1)])
dy<-diff(y[i:(i+1)])
if(dx && dy) {
if((dx+lastdx)/(dy+lastdy) < 0) {
newx<-c(newx,x[i]+dx,x[i]+dx)
newy<-c(newy,y[i],y[i]+dy)
}
else {
newx<-c(newx,x[i],x[i]+dx)
newy<-c(newy,y[i]+dy,y[i]+dy)
}
}
else {
newx<-c(newx,x[i+1])
newy<-c(newy,y[i+1])
}
lastdx<-dx
lastdy<-dy
}
return(list(x=newx,y=newy))
}
x<-c(5,4,4,3,2,2,1,1,2,2,3,4,5,5,6,6,7,8,8,9,8,7,7,6,6,5,5)
y<-c(1,2,2,3,4,4,5,6,6,7,8,8,9,8,8,7,7,6,6,5,5,4,3,3,2,2,1)
plot(1:9,type="n")
lines(x,y)
newxy<-pixel8(x,y)
lines(newxy$x,newxy$y,col="red")
Jim
On Fri, Aug 5, 2016 at 11:40 AM, Jim Lemon <drjimlemon at gmail.com> wrote:
> Hi Zun Yin,
> The first problem requires something like this:
>
> pixel8<-function(x,y,pixsize=1) {
> nsteps<-length(x)-1
> newx<-x[1]
> newy<-y[1]
> for(i in 1:nsteps) {
> dx<-diff(x[i:(i+1)])
> dy<-diff(y[i:(i+1)])
> if(dx && dy) {
> newx<-c(newx,x[i]+dx,x[i]+dx)
> newy<-c(newy,y[i],y[i]+dy)
> }
> else {
> newx<-c(newx,x[i+1])
> newy<-c(newy,y[i+1])
> }
> }
> return(list(x=newx,y=newy))
> }
>
> I think that this does part of what you want. Your points seem to be
> in the middle of a pixel edge, so an offset would have to be added to
> align the resulting points with the corners. The other thing to work
> out is the order of changing x and y on a slope, which I think is a
> function of whether the line is "inside" or "outside" the overall area
> enclosed. I'll post again if I have any brilliant ideas.
>
> Jim
>
>
> On Fri, Aug 5, 2016 at 6:08 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
>> On 04/08/2016 11:46 AM, Zun Yin wrote:
>>>
>>> Dear William, Duncan and Bert,
>>>
>>> Thanks a lot for your help and tips :D In fact the contour() plot still
>>> cannot solve my problem perfectly. As the resolution of my plot is very
>>> coarse, I want to a contour perfectly surround all grid cells in the river
>>> basin. In another word, I want all angle of the contour to be 90 degree.
>>> Another problem for the contour() is that the contour line doesn't close
>>> at
>>> where the value of neighbouring grid cell is NA. See the right side of the
>>> two contour lines (attachment). Do you know how I can get what I want?
>>> Thanks a lot :P
>>
>>
>> The second problem is easy: just change your NA values to some new ID
>> value, or change the test from
>>
>> basiinID == ID
>>
>> to
>>
>> !is.na(basiinID) & basiinID == ID
>>
>> The first one looks harder.
>>
>> Duncan Murdoch
>>
>>
>>>
>>> Cheers,
>>>
>>>
>>> Zun Yin
>>>
>>> On Thu, Aug 4, 2016 at 5:09 PM, Bert Gunter <bgunter.4567 at gmail.com>
>>> wrote:
>>>
>>>> ... note the typo. It's:
>>>>
>>>> contour( basiinID == ID, level=0.5)
>>>>
>>>> :-)
>>>>
>>>>
>>>> -- Bert
>>>>
>>>>
>>>>
>>>> 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, Aug 4, 2016 at 8:03 AM, William Dunlap via R-help
>>>> <r-help at r-project.org> wrote:
>>>>>
>>>>> If 'basinID' is the matrix of basin identifiers you could draw an
>>>>> outline
>>>>> of the basin with identifier 'ID' with
>>>>> coutour( basiinID == ID, level=0.5)
>>>>> Add 'add=TRUE' if you are overlaying this on an existing plot.
>>>>>
>>>>> Bill Dunlap
>>>>> TIBCO Software
>>>>> wdunlap tibco.com
>>>>>
>>>>> On Thu, Aug 4, 2016 at 1:51 AM, Zun Yin <yinzun2000 at gmail.com> wrote:
>>>>>
>>>>>> D
>>>>>> ear all,
>>>>>>
>>>>>> I have a matrix with ID of river basins (integer numbers). Now I want
>>>>>> to
>>>>>> highlight one river basin in a map by plotting only the border. Like
>>>>>> the
>>>>>> attached figure. Two river basins are highlighted by polygons. It is
>>>>>> created by ferret, but I prefer to implement it by R. Anybody know how
>>>>
>>>> to
>>>>>>
>>>>>> do it? Thanks a lot.
>>>>>>
>>>>>> Cheers,
>>>>>>
>>>>>> Zun Yin
>>>>>>
>>>>>> ______________________________________________
>>>>>> R-help at 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]]
>>>>>
>>>>> ______________________________________________
>>>>> R-help at 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 at 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 at 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.
More information about the R-help
mailing list