[R] add points to lattice cloud plot (3D scatter)

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Thu Feb 28 22:09:59 CET 2019


On 2/28/19 7:56 AM, Jeff Newmiller wrote:
> You are missing the point... lattice assembles the entire data set at once so it can adjust and synchronize all of the scales and then it generates an object that can be printed to a device. This approach is entirely incompatible with the base graphics approach of keeping global variables around that help successive functions cooperate to cumulatively build up an image.
>
> You need to get all your points into the lattice call initially. which may involve changing how you structure the data before you create the plot. Read the vignette and some tutorials.


There is an panel.identify.cloud function which will let one interact 
with a plot as one can do with identify or panel.identify. It calls a 
function that you can view with:


getAnywhere(panel.3didentify)


That said, this is only discussing the possibility of adding to an 
existing 3d plot in theory. I've never done it. I have used some of the 
other "interaction" plotting functions to add lines and curves to 
existing plot objects. You can see the options that have been described in:

?panel.identify.cloud
  ?llines

I tried search for prior question on Rhelp but could only find examples 
where annotation of existing points was attempted, not any examples of 
adding points. I wondered if one could do it by simply editing the 
x,y,z  components of an existing lattice object:


 From object created from the iris dataset:

$ panel.args.common:List of 20
   ..$ x           : num [1:150] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
   ..$ y           : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
   ..$ z           : num [1:150] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...


I tried working with the original example in this thread but originally 
got an error that on traceback stated that there was a problem with 
arguments to latticeParseFormula. I didn't see why that would be, but 
taking out the `ltext` call allowed completion. I then only succeeded in 
modifying the plot when I prepended new data to the existing vectors, 
but not when I appended points. Perhaps there is a counter in the 
lattice object that I have not yet seen.


Hope this helps;

David.

>
> On February 28, 2019 7:38:52 AM PST, Luigi Marongiu <marongiu.luigi using gmail.com> wrote:
>> I see. I have been thinking of superimposing two plots with
>> par(new=TRUE), but how could I remove all the graphic parameters
>> (axes, background etc) keeping only the actual points in lattice? (if
>> possible).
>> Tx
>>
>> On Thu, Feb 28, 2019 at 3:53 PM Duncan Murdoch
>> <murdoch.duncan using gmail.com> wrote:
>>> On 28/02/2019 5:39 a.m., Luigi Marongiu wrote:
>>>> Dear all,
>>>> is it possible to add points to a lattice cloud plot (3D scatter)?
>> I
>>>> can plot the main data, but what if I wanted to add another point.
>> In
>>>> R there is the high level plotting function plot(), then the low
>> level
>>>> points() or lines() etc. What is the equivalent for lattice?
>>> I don't know for sure, but I don't think you can do that in lattice.
>>> The scatterplot3d::scatterplot3d function returns enough information
>> to
>>> do this, but I don't think lattice::cloud does.  But even
>>> scatterplot3d::scatterplot3d won't necessarily get it right if points
>>> hide others that are behind them.  It uses the "painter's algorithm",
>>> and that needs everything to be drawn in just the right order, which
>> you
>>> probably won't get if you draw things in several calls.
>>>
>>> You can draw things in arbitrary order using rgl::plot3d or related
>>> functions, but you'll need to do more work yourself to get an array
>> of
>>> plots like lattice gives.
>>>
>>> Duncan Murdoch
>>>
>>>
>>>> Thank you
>>>>
>>>>
>>>> df = data.frame(Name = c("A", "B", "C", "D", "E"),
>>>>                 x_axis = c(-0.591, 0.384, -0.384, -0.032, 0.754),
>>>>                 y_axis = c(-1.302, 1.652, -1.652, 0.326, 0.652),
>>>>                 z_axis = c(1.33, 1.33, 2.213, 0.032, -0.754),
>>>>                 stringsAsFactors = FALSE)
>>>>
>>>> cloud(z_axis ~ x_axis * y_axis, data = df,
>>>>         xlab = "X", ylab = "Y", zlab = "Z",
>>>>         pch = 16, col = "red", type = "b", cex = 1.5,
>>>>         ltext(x=df$x_axis, y=df$y_axis, z=df$z_axis,
>>>>               labels=df$Names, pos=1, offset=1, cex=0.8)
>>>> )
>>>>
>>>> df2 = data.frame(Name = "F",
>>>>                   x_axis = 0.891,
>>>>                   y_axis = 2.302
>>>>                   z_axis = -1.83,
>>>>                   stringsAsFactors = FALSE)
>>>>



More information about the R-help mailing list