-1

How to do the figure shown in link 1 in Matlab or R?I have searched in google and stackoverflow, but have not found what I need.

enter image description here

What I need is to overlay box plot on scatter plot in Matlab or R to show data distribution, or are there any other methods or sotfwares to realize what I need?

This is my first time to ask questions in stackoverflow, and my English is very general that maybe I have done something not appropriate. So, please forgive me and help me. O(∩_∩)O~

How can I overlay box plot on scatter plot in matlab or R?

thelatemail
  • 81,120
  • 12
  • 111
  • 172
Jedi1985
  • 3
  • 4

2 Answers2

2

In R, you could do something like Maybe something like

#sample data
dd<-data.frame(x=runif(100))
br=seq(0,1,length.out=6)
cent<-(br[-1] +br[-length(br)])/2
dd<-transform(dd, y = x+rnorm(100, .2, .05),
   xcut = cut(x, breaks=br))

#plot
plot(y~x, dd)
boxplot(y~xcut, dd, add=T, at=cent, boxwex=(br[2]-br[1]) * 3/4, xaxt="n")

which produces

enter image description here

The trick is just to use cut() along the x-axis to break the range into discrete chunks that you want to draw box plots for. Then you just plot on top and line things up.

MrFlick
  • 163,738
  • 12
  • 226
  • 242
  • Yes, it is useful and I have realized what you give in Matlab before I ask this question. But the result is still different from what I need. Can the box be thin and the figure be colorful like the reference given?The figure I need is used in many papers and maybe there are some methods to realized it in a easy way. – Jedi1985 Sep 24 '14 at 03:17
  • For examples: Simini, F., Gonz A Lez, M. C., Maritan, A. and Barab A Si, A. L. (2012). "A universal model for mobility and migration patterns." Nature. and Liu, Y., Sui, Z., Kang, C. and Gao, Y. (2014). "Uncovering Patterns of Inter-Urban Trip and Spatial Interaction from Social Media Check-In Data." PLoS ONE 9(1): e86026. – Jedi1985 Sep 24 '14 at 03:20
  • Sure. Read the `?boxplot` help page for more details. You weren't super descriptive in your question as to what exactly was needed. I just wanted to get you pointed in the right direction. I'm hopefully you can customize it form here. If you get stuck again, ask a different, more specific Stack Overflow question. Make sure you also include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with your question that has data similar to the ones you wish to plot. – MrFlick Sep 24 '14 at 03:21
  • OK!Thank you very much!Because this is my time to ask question and my poor English language ability limits my expression, I ask this question in a inappropriate way. I will improve in future!Thanka a lot! O(∩_∩)O~ – Jedi1985 Sep 24 '14 at 03:28
0

This is my Matlab codes to realize what I need but I still do not satisfy.

function handle = scatterbox(x,y,n,varargin)
% handle = sactter_box(x,y,n[,'pointsize',size,'pointcolor',color,'pointmarker',marker])
% x——x;
% y——y;
% n——the number of boxes;
% pointsize——point size(default value is 100);
% pointcolor——point color(default value is [hex2dec('ad')/256 hex2dec('ad')/256 hex2dec('ad')/256]);
% pointmarker——point marker(default value is '.');

%% 检查参数输入个数,并读取可选参数
if nargin<3
error('至少包含x、y和n等3个必要参数!');
elseif nargin>3 && nargin<9 && mod(nargin,2)==0
error('参数输入应为3、5、7或9个!')
elseif nargin>9
error('参数输入超过最大9个!')
else
%设置可选参数默认值
pointsize    = 36;
pointcolor   = [hex2dec('ad')/256 hex2dec('ad')/256 hex2dec('ad')/256];
pointmarker  = '.';

%读取可选参数
Arguments = varargin;
while length(Arguments)>=2 %设置循环条件
Property = Arguments{1}; %获取参数名称
Value = Arguments{2}; %获取参数值
Arguments = Arguments(3:end); %循环设置,取剩余参数
switch Property %判断可选参数
case 'pointsize'
if isa(Value,'numeric')
pointsize = Value;
else
error('size参数应为数字!')
end
case 'pointcolor'
if find(strcmp({'y' 'm' 'c' 'r' 'g' 'b' 'w' 'k'},Value))    %用字母表示颜色
pointcolor = Value;
elseif isa(Value,'numeric') && sum(size(Value)==[1 3])==2   %用二维矩阵表示颜色
pointcolor = Value;
else
error('color参数应为颜色格式!')
end
case 'pointmarker'
if find(strcmp({'o' '+' '*' '.' 'x' 's' 'd' '^' 'v' '>' '<' 'p' 'h' 'none'},Value))
pointmarker = Value;
else
error('marker参数应为符号字符!')
end
otherwise
error('参数名输入出错!');
end
end
end

%% scatterbox
handle = scatter(x,y,pointsize,pointcolor,pointmarker);    %作散点图

limits = axis(gca); %获取坐标范围
box_datarange = (limits(2)-limits(1))/n;    %获取箱线图对应数据范围
box_position = limits(1)-box_datarange/2+box_datarange*[1:n];   %获取箱线图x轴位置

hold on;
for i=1:n    %通过循环作箱线图
box_data = y(x>=box_position(i)-box_datarange/2 & x<box_position(i)+box_datarange/2);   %获取箱线图数据
if length(box_data)>3   %仅当数据量大于3时作箱线图
boxplot(box_data,...
'positions',box_position(i),... %设置箱线图位置
'plotstyle','compact',...   %设置箱线图样式
'colors',[0 hex2dec('91')/256 0],...    %设置箱线图颜色
'symbol','');    %设置异常值散点符号为空,即不显示异常值
set(gca,'XTickLabel',{''}); %清除x轴箱线图产生标注
end
end

axis(gca,limits);   %恢复原坐标范围
set(gca,'XTickLabelMode','auto','XTickMode','auto');    %恢复默认刻度和标注

end
Jedi1985
  • 3
  • 4