我想知道是否有与 R 中的lattice包中的xyplot()
函数类似的函数。我想在不同的图表中绘制按子组分类的观察组。
例如,如果我有以下数据框:
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))
我正在寻找以下输出:
library(lattice)
xyplot(d$ValueY ~ d$ValueX | d$Group, group=as.factor(d$Subgroup),
auto.key=list(space="right", points=T), pch=20)
在 MATLAB 中,我将 d
定义为数据集,并使用 sbiotrellis
函数绘制了以下内容:
sbiotrellis(d,'Group','ValueX','ValueY')
但是,并未考虑这些子组。有什么想法吗?
最佳答案
R:
write.table(d, file = "myfile.csv", sep = ",", row.names = FALSE)
Matlab:
操作系统
lsb_release -i -r
Distributor ID: RedHatEnterpriseServer
Release: 6.5
Matlab 版本和发布
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
数据
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
根据子组变量将数据分组
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, :);
绘制网格图。使用hold on
将多个图层绘制到一个图形上
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';
到目前为止的情节:
捕获 gcf,它为当前图窗的父级提供句柄
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
使用父句柄,获取其子句柄的属性
bx1 = bx.Children
% bx1 =
%
% 6x1 graphics array:
%
% Axes (Group "group4")
% Axes (Group "group3")
% Axes (Group "group1")
% Legend (ValueY, ValueY)
% Axes (Group "group2")
% Axes (BackgroundAxes)
现在,您可以看到 bx 句柄有 6 个子项(5 个轴和 1 个图例)。可以按如下方式操纵子项的属性。
要获取每个子项的参数列表,请执行
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'
要获取子项的参数值,请执行以下操作
get(bx1(1), 'ZGrid')
%ans =
%
%off
要自定义每个子项的参数,请执行
set(bx1(1), 'ZGrid', 'on')
自定义子图、图例和标题的属性
%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]);
评估子句柄后,我们会看到该解决方案末尾显示的最终图。
图形的其他属性也可以修改,如果您查看 sbiotrellis 的源代码('编辑 sbiotrellis'
)或键入绘图对象,后跟字母和制表符完成(t2.u
),您可以获得渲染绘图后可以操作的属性或隐藏属性的列表。
属性
hFig;
nPlots;
plots;
属性(隐藏)
layoutObj;
labelx = '';
labely = '';
plottitle = '';
legendnames = {};
plotLegend;
rows;
cols;
titleFontSize = 8;
axisLimitSlack = .05
backgroundInsets = [30 30 30 30];
foregroundInsets = [50 50 05 35];
输出:
R - 格子 - xyplot:
Matlab - sbiotrellis:
关于r - MATLAB 绘图函数类似于 Lattice 包中的 xyplot(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29526461/