Quantcast
Channel: Yet Another Math Programming Consultant
Viewing all articles
Browse latest Browse all 805

ggplot example

$
0
0

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 wit distance to Utopia point to df
>
#
>
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,c("Cost","Weight","Performance")]
>
C=rbind(C,U)
>

> 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"))+
+
geom_point(x=U$Cost,y=U$Performance,size=3,color='black')+
+
geom_path(data=C,x=C$Cost,y=C$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"))+
+
geom_point(x=U$Weight,y=U$Performance,size=3,color='black')+
+
geom_path(data=C,x=C$Weight,y=C$Performance)
Rplot03

Rplot04


Viewing all articles
Browse latest Browse all 805

Latest Images

Trending Articles



Latest Images