Friday, January 22, 2016

ggplot example

To produce high-quality plots for a document R’s ggplot is always a good choice:

> library(ggplot2)
> df<- read.csv("c:\\tmp\\testsolution.csv")
> head(df)
  ID Performance    Cost Weight
1  1      0.7051 4365766  49595
2  2      0.7071 4366262  50335
3  3      0.7091 4367526  51091
4  4      0.7110 4368147  50242
5  5      0.7130 4369411  50998
6  6      0.7149 4372412  53125
> ggplot(data=df,aes(x=Cost,y=Performance))+geom_point(aes(color=Weight))+scale_color_gradientn(colors = rainbow(5))
> ggplot(data=df,aes(x=Weight,y=Performance))+geom_point(aes(color=Cost))+scale_color_gradientn(colors = rainbow(5))

image

image

Update

After some fine tuning:

  • Reverse coloring scheme (red=bad, i.e. heavy or expensive)
  • Add Utopia point
  • Add line from Utopia point to Compromise point (smallest normalized distance)
  • Longer legend

 

> library(ggplot2)
> df<- read.csv("c:\\tmp\\testsolution.csv")
> #
> # Utopia Point
> #
> U<-data.frame("Cost"=min(df$Cost),
+               "Weight"=min(df$Weight),
+               "Performance"=max(df$Performance))
> #
> # Ranges
> #
> R<-data.frame("Cost"=max(df$Cost)-min(df$Cost),
+               "Weight"=max(df$Weight)-min(df$Weight),
+               "Performance"=max(df$Performance)-min(df$Performance))
> # 
> # add column to df with distance from Utopia point
> #
> df$Distance = sqrt(
+   ((df$Cost-U$Cost)/R$Cost)^2+
+     ((df$Weight-U$Weight)/R$Weight)^2+
+     ((df$Performance-U$Performance)/R$Performance)^2
+ )
> #
> # Compromise point
> #
> mindist = min(df$Distance)
> C=df[df$Distance==mindist,]
> 
> 
> ggplot(data=df,aes(x=Cost,y=Performance))+
+   geom_point(aes(color=Weight))+
+   scale_color_gradientn(colors = rev(rainbow(5)))+
+   ggtitle("testsolution.csv")+
+   theme(legend.key.height=unit(4, "line"))+
+   annotate("point",x=U$Cost,y=U$Performance,size=3,color='black')+
+   annotate("segment",x=C$Cost,y=C$Performance,xend=U$Cost,yend=U$Performance)
> ggplot(data=df,aes(x=Weight,y=Performance))+
+   geom_point(aes(color=Cost))+
+   scale_color_gradientn(colors = rev(rainbow(5)))+
+   ggtitle("testsolution.csv")+
+   theme(legend.key.height=unit(4, "line"))+
+   annotate("point",x=U$Weight,y=U$Performance,size=3,color='black')+
+   annotate("segment",x=C$Weight,y=C$Performance,xend=U$Weight,yend=U$Performance)
Rplot03

Rplot04

Another interesting picture is to color by distance:

Rplot05

No comments:

Post a Comment