-
Notifications
You must be signed in to change notification settings - Fork 0
/
scatterPlot.r
49 lines (37 loc) · 1.11 KB
/
scatterPlot.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
plotScatterChart <- function(data,x,y,title,scale=y,h=6,w=6,color=FALSE,line=FALSE,facet=NULL,professional=FALSE) {
require(ggplot2);
require(ggthemes);
plot <- ggplot(data, aes_string(x=x, y=y)) +
xlab(x) +
ylab(y) +
ggtitle(title);
if(color) {
plot <- plot +
geom_point(aes_string(color=scale),alpha=0.8) +
scale_color_gradient(low="lightblue",high="navy");
if(line){
plot <- plot +
geom_smooth(method="lm",fill="red",color="purple")
}
} else {
plot <- plot +
geom_point();
if(line){
plot <- plot +
geom_smooth(method="lm",color="red");
}
}
if(!is.null(facet)){
facets <- as.formula(paste("~",facet))
plot <- plot + facet_wrap(facets);
}
if(professional){
plot <- plot +
geom_rangeframe() +
theme_tufte();
}
ggsave(file="scatter_plot.svg", plot=plot, height=h, width=w);
return(plot);
}
#plotScatterChart(data=diamonds,x="price",y="carat",title="My Graph",facet="cut",color=TRUE,line=TRUE,professional=TRUE)
plotScatterChart(data=diamonds,x="price",y="carat",title="My Graph",facet="cut",professional=TRUE,line=TRUE,color=TRUE)