Reputation: 787
I would like to know if there is any similar function to the xyplot()
function from the lattice package in R. I would like to plot groups of observations classified by subgroups in different charts.
For example, if I have the following dataframe:
d <- data.frame(ID=paste0("id",1:40),
Group=paste0("group",rep(1:4,10)),
Subgroup=paste0("subgroup",c(rep(c("A","B","B"),13),"A")),
ValueX=rnorm(40),
ValueY=rnorm(40))
I am looking for the following output:
library(lattice)
xyplot(d$ValueY ~ d$ValueX | d$Group, group=as.factor(d$Subgroup),
auto.key=list(space="right", points=T), pch=20)
In MATLAB, I defined d
as a dataset and plotted the following using sbiotrellis
function:
sbiotrellis(d,'Group','ValueX','ValueY')
However, the subgroups are not being considered. Any ideas?
Upvotes: 2
Views: 819
Reputation: 12723
R:
write.table(d, file = "myfile.csv", sep = ",", row.names = FALSE)
Matlab:
Operating System
lsb_release -i -r
Distributor ID: RedHatEnterpriseServer
Release: 6.5
Matlab Version and Release
version -date
% ans =
%
% September 15, 2014
version -release
% ans =
%
% 2014b
version -java
%ans =
%
%Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
Data
ds = dataset('File', 'myfile.csv','Delimiter',',', 'HeaderLines', 1, 'ReadVarNames', false, 'ReadObsNames', true);
vn = {'ID', 'Group', 'Subgroup', 'ValueX', 'ValueY'};
ds = set(ds, 'VarNames', vn);
ds
% ds =
%
% ID Group Subgroup ValueX ValueY
% "1" '"id1"' '"group1"' '"subgroupA"' 0.5524 -0.21252
% "2" '"id2"' '"group2"' '"subgroupB"' -0.30546 -0.12646
% "3" '"id3"' '"group3"' '"subgroupB"' -0.96933 -1.0407
% "4" '"id4"' '"group4"' '"subgroupA"' 0.024616 -1.1806
% "5" '"id5"' '"group1"' '"subgroupB"' 0.3268 -0.97485
% "6" '"id6"' '"group2"' '"subgroupB"' 0.55165 -0.70415
% "7" '"id7"' '"group3"' '"subgroupA"' 0.36608 -0.97104
% "8" '"id8"' '"group4"' '"subgroupB"' 0.028234 0.26336
% "9" '"id9"' '"group1"' '"subgroupB"' 0.71088 0.97357
% "10" '"id10"' '"group2"' '"subgroupA"' -0.14237 -0.0085301
% "11" '"id11"' '"group3"' '"subgroupB"' 1.1259 -0.85614
% "12" '"id12"' '"group4"' '"subgroupB"' 0.73305 0.33118
% "13" '"id13"' '"group1"' '"subgroupA"' 0.41297 -0.28922
% "14" '"id14"' '"group2"' '"subgroupB"' -0.41621 0.35157
% "15" '"id15"' '"group3"' '"subgroupB"' -0.48856 0.85537
% "16" '"id16"' '"group4"' '"subgroupA"' -1.8682 0.6652
% "17" '"id17"' '"group1"' '"subgroupB"' -0.098612 1.563
% "18" '"id18"' '"group2"' '"subgroupB"' -0.53824 0.57049
% "19" '"id19"' '"group3"' '"subgroupA"' 0.24112 0.168
% "20" '"id20"' '"group4"' '"subgroupB"' -0.35454 -1.23
% "21" '"id21"' '"group1"' '"subgroupB"' -0.45688 1.2703
% "22" '"id22"' '"group2"' '"subgroupA"' 0.68444 -1.3548
% "23" '"id23"' '"group3"' '"subgroupB"' -1.1469 2.1724
% "24" '"id24"' '"group4"' '"subgroupB"' -0.035062 -0.41989
% "25" '"id25"' '"group1"' '"subgroupA"' -1.2422 1.2415
% "26" '"id26"' '"group2"' '"subgroupB"' 2.1308 -0.76941
% "27" '"id27"' '"group3"' '"subgroupB"' -0.87732 -0.9218
% "28" '"id28"' '"group4"' '"subgroupA"' 2.14 0.12569
% "29" '"id29"' '"group1"' '"subgroupB"' 0.13455 -0.22301
% "30" '"id30"' '"group2"' '"subgroupB"' -1.0742 -0.35052
% "31" '"id31"' '"group3"' '"subgroupA"' -1.121 0.55119
% "32" '"id32"' '"group4"' '"subgroupB"' 0.88677 -1.1549
% "33" '"id33"' '"group1"' '"subgroupB"' -0.28575 1.1066
% "34" '"id34"' '"group2"' '"subgroupA"' -0.10857 -2.0048
% "35" '"id35"' '"group3"' '"subgroupB"' -0.78056 0.23084
% "36" '"id36"' '"group4"' '"subgroupB"' -0.16794 -0.48823
% "37" '"id37"' '"group1"' '"subgroupA"' -0.080923 0.309
% "38" '"id38"' '"group2"' '"subgroupB"' 1.7291 -1.0569
% "39" '"id39"' '"group3"' '"subgroupB"' -1.2291 -1.7684
% "40" '"id40"' '"group4"' '"subgroupA"' -1.3804 1.4364
Divide the data into groups based on Subgroup variable
target = 'subgroupA';
vi = find(cellfun('length', regexp(ds.Subgroup, target)) == 1);
data1 = ds(vi, :);
target = 'subgroupB';
vi = find(cellfun('length', regexp(ds.Subgroup, target)) == 1);
data2 = ds(vi, :);
Plot trellis plot. Use hold on
to plot multiple layers on to one figure
t1 = sbiotrellis(data1, 'Group', 'ValueX', 'ValueY', 'LineStyle', 'none', 'Marker', 'o', 'MarkerEdgeColor', [1,0,0], 'MarkerSize', 4, 'MarkerFaceColor', [1,0,0]);
hold on
t2 = sbiotrellis(data2, 'Group', 'ValueX', 'ValueY', 'LineStyle', 'none', 'Marker', 'o', 'MarkerEdgeColor', [0,0,1], 'MarkerSize', 4, 'MarkerFaceColor', [0,0,1]);
t2.labelx = 'X';
t2.labely = 'Y';
t2.plottitle = 'My Trellis Plot';
Plot upto this point:
Capture gcf which gives the handle to the parent of the current figure
bx = gcf
% bx =
%
% Figure (SimBiologyTrellisPlot) with properties:
%
% Number: 2
% Name: ''
% Color: [0.9400 0.9400 0.9400]
% Position: [415 124 518 384]
% Units: 'pixels'
%
% Show all properties
Using the parent handle, get the properties of its children
bx1 = bx.Children
% bx1 =
%
% 6x1 graphics array:
%
% Axes (Group "group4")
% Axes (Group "group3")
% Axes (Group "group1")
% Legend (ValueY, ValueY)
% Axes (Group "group2")
% Axes (BackgroundAxes)
Now, you could see that there are 6 children (5 Axes and 1 legend) for bx handle. The properties of children could be manipulated as follows.
To get the list of parameters for each child, do
get(bx1(1))
% ALim: [0 1]
% ALimMode: 'auto'
% ActivePositionProperty: 'position'
% AmbientLightColor: [1 1 1]
% BeingDeleted: 'off'
% Box: 'on'
% BoxStyle: 'back'
% BusyAction: 'queue'
% ButtonDownFcn: {3x1 cell}
% CLim: [0 1]
% CLimMode: 'auto'
% CameraPosition: [0.1359 0.0838 17.3205]
% CameraPositionMode: 'auto'
% CameraTarget: [0.1359 0.0838 0]
% CameraTargetMode: 'auto'
% CameraUpVector: [0 1 0]
% CameraUpVectorMode: 'auto'
% CameraViewAngle: 6.6086
% CameraViewAngleMode: 'auto'
% Children: [2x1 Line]
% Clipping: 'on'
% ClippingStyle: '3dbox'
% Color: [1 1 1]
% ColorOrder: [7x3 double]
% ColorOrderIndex: 3
% CreateFcn: ''
% CurrentPoint: [2x3 double]
% DataAspectRatio: [2.2045 2.2974 1]
% DataAspectRatioMode: 'auto'
% DeleteFcn: ''
% FontAngle: 'normal'
% FontName: 'Helvetica'
% FontSize: 8
% FontSmoothing: 'on'
% FontUnits: 'points'
% FontWeight: 'normal'
% GridAlpha: 0.1500
% GridAlphaMode: 'auto'
% GridColor: [0.1500 0.1500 0.1500]
% GridColorMode: 'auto'
% GridLineStyle: '-'
% HandleVisibility: 'on'
% HitTest: 'on'
% Interruptible: 'on'
% LabelFontSizeMultiplier: 1.1000
% Layer: 'bottom'
% LineStyleOrder: '-'
% LineStyleOrderIndex: 1
% LineWidth: 0.5000
% MinorGridAlpha: 0.2500
% MinorGridAlphaMode: 'auto'
% MinorGridColor: [0.1000 0.1000 0.1000]
% MinorGridLineStyle: ':'
% NextPlot: 'replace'
% OuterPosition: [172.4400 6.7600 289.2000 211.0400]
% Parent: [1x1 Figure]
% PickableParts: 'visible'
% PlotBoxAspectRatio: [1 0.8046 0.8046]
% PlotBoxAspectRatioMode: 'auto'
% Position: [239 49 174 140]
% Projection: 'orthographic'
% Selected: 'off'
% SelectionHighlight: 'on'
% SortMethod: 'childorder'
% Tag: ''
% TickDir: 'in'
% TickDirMode: 'auto'
% TickLabelInterpreter: 'tex'
% TickLength: [0.0100 0.0250]
% TightInset: [0 1.4852e-06 0 0]
% Title: [1x1 Text]
% TitleFontSizeMultiplier: 1.1000
% TitleFontWeight: 'bold'
% Type: 'axes'
% UIContextMenu: []
% Units: 'pixels'
% UserData: []
% View: [0 90]
% Visible: 'on'
% XAxisLocation: 'bottom'
% XColor: [0.1500 0.1500 0.1500]
% XColorMode: 'auto'
% XDir: 'normal'
% XGrid: 'off'
% XLabel: [1x1 Text]
% XLim: [-2.0686 2.3405]
% XLimMode: 'manual'
% XMinorGrid: 'off'
% XMinorTick: 'off'
% XScale: 'linear'
% XTick: [-2 -1 0 1 2]
% XTickLabel: ''
% XTickLabelMode: 'manual'
% XTickLabelRotation: 0
% XTickMode: 'auto'
% YAxisLocation: 'left'
% YColor: [0.1500 0.1500 0.1500]
% YColorMode: 'auto'
% YDir: 'normal'
% YGrid: 'off'
% YLabel: [1x1 Text]
% YLim: [-2.2136 2.3813]
% YLimMode: 'manual'
% YMinorGrid: 'off'
% YMinorTick: 'off'
% YScale: 'linear'
% YTick: [-2 -1 0 1 2]
% YTickLabel: ''
% YTickLabelMode: 'manual'
% YTickLabelRotation: 0
% YTickMode: 'auto'
% ZColor: [0.1500 0.1500 0.1500]
% ZColorMode: 'auto'
% ZDir: 'normal'
% ZGrid: 'off'
% ZLabel: [1x1 Text]
% ZLim: [-1 1]
% ZLimMode: 'auto'
% ZMinorGrid: 'off'
% ZMinorTick: 'off'
% ZScale: 'linear'
% ZTick: [-1 0 1]
% ZTickLabel: ''
% ZTickLabelMode: 'auto'
% ZTickLabelRotation: 0
% ZTickMode: 'auto'
To get the value of paramater of a child, do
get(bx1(1), 'ZGrid')
%ans =
%
%off
To customize parameters of each child, do
set(bx1(1), 'ZGrid', 'on')
Customize properties of subplots, legend, and title
%subplot 4
bx1(1).Title.String = 'Group 4';
bx1(1).Title.Position = [0.1359, 2.6, 0];
bx1(1).Title.FontWeight = 'bold';
%subplot 3
bx1(2).Title.String = 'Group 3';
bx1(2).Title.Position = [0.1359, 2.6, 0];
bx1(2).Title.FontWeight = 'bold';
%subplot 1
bx1(3).Title.String = 'Group 1';
bx1(3).Title.Position = [0.1359, 2.6, 0];
bx1(3).Title.FontWeight = 'bold';
%legend text, box, and location
bx1(4).String = {'SubgroupA', 'SubgroupB'};
bx1(4).Position = [397.2451 185 109.7549 29.2715];
h =findobj('type', 'legend');
set(h, 'Box', 'off');
%subplot 2
bx1(5).Title.String = 'Group 2';
bx1(5).Title.Position = [0.1359, 2.6, 0];
bx1(5).Title.FontWeight = 'bold';
% Title location
set(bx1(6), 'Position', [30, 30, 390, 324]);
After evaluating the children handles, we see a final plot shown at the end of this solution.
Other properties of the figure can be also modified and if you look at the source code of sbiotrellis ('edit sbiotrellis'
) or typing the plot object followed by a letter and tab completion (t2.u
), you could get the list of properties or hidden properties that can be manipulated after rendering the plot.
properties
hFig;
nPlots;
plots;
properties (Hidden)
layoutObj;
labelx = '';
labely = '';
plottitle = '';
legendnames = {};
plotLegend;
rows;
cols;
titleFontSize = 8;
axisLimitSlack = .05
backgroundInsets = [30 30 30 30];
foregroundInsets = [50 50 05 35];
Output:
R - Lattice - xyplot:
Matlab - sbiotrellis:
Upvotes: 2