2012年8月25日 星期六

R ggplot2 code

My data is like this :








Read it and input to R, use the ggplot2

library(ggplot2)
polar <- read.table("test.txt", header=F)
attach(polar)

Set the variable 

distance <- polar$V1
angle <- polar$V2


Draw the polar plot 

ggplot( polar, aes(x=angle, y=distance )) + coord_polar(start=0) + geom_point() +
  scale_x_continuous(breaks=seq(0, 360, by=30), expand=c(0,0), lim=c(0, 360))+
  scale_area()


output

like this

But the "0 /360" is in the top, how to rotate to the right land ( in the direction of 90 degree ) ?

























Fixed code

In this "readme" link,  http://had.co.nz/ggplot2/coord_polar.html
  • start    : offset from 12 o'clock in radians
  • direction: 1, clockwise ;   -1, anticlockwise

ggplot(polar, aes(x=angle, y=distance )) + coord_polar( start = 90,  direction = -1) +
   geom_point() + scale_x_continuous(breaks=seq(0, 360, by=30), expand=c(0,0),
   lim=c(0, 360)) + scale_area()


why add it, still no function !!

























The error is " start : offset from 12 o'clock in radians  "

so code change

ggplot(polar, aes(x=angle, y=distance )) + coord_polar( start = 1.57,  direction = -1) +
   geom_point() + scale_x_continuous(breaks=seq(0, 360, by=30), expand=c(0,0),
   lim=c(0, 360)) + scale_area()


























the start is the offset in radians (not degrees) and
 you want to rotate it clockwise (so direction=1).

Link here !!

沒有留言: