开发者

ggplot 2 - Race track plot error

开发者 https://www.devze.com 2023-03-23 18:08 出处:网络
I was trying to reproduce the Race track plot from ggplot2 (http://had.co.nz/ggplot2/co开发者_JAVA百科ord_polar.html) on my own data.

I was trying to reproduce the Race track plot from ggplot2 (http://had.co.nz/ggplot2/co开发者_JAVA百科ord_polar.html) on my own data.

> y[1:10,]
         cd cnt day year
1  2003-10-18   3 291 2003
2  2003-10-20   1 293 2003
3  2003-10-21   1 294 2003
4  2003-10-22   1 295 2003
5  2003-10-23   2 296 2003
6  2003-11-03   1 307 2003
7  2003-11-18   3 322 2003
8  2003-11-20   2 324 2003
9  2003-11-21   6 325 2003
10 2003-11-24   1 328 2003
> q <- ggplot(y, aes(y$day, y$year, fill=y$cnt)) + geom_bar(width = 0.9, position = "fill") + coord_polar(theta="x")
> q
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
Error in as.Date.numeric(value) : 'origin' must be supplied
In addition: Warning message:
In cbind(is.na(mmm), is.na(each)) :
  number of rows of result is not a multiple of vector length (arg 2)

But I am getting this error and I can not find any work around. Where does the 'origin' come in? Could anyone give me a hand on this? Thanks folks.

And here goes the output of str(y):

'data.frame':   2430 obs. of  4 variables:
 $ cd  :Class 'Date'  num [1:2430] 12343 12345 12346 12347 12348 ...
 $ cnt : int  3 1 1 1 2 1 3 2 6 1 ...
 $ day : num  291 293 294 295 296 307 322 324 325 328 ...
 $ year: num  2003 2003 2003 2003 2003 ...


As Andrie mentioned, your error isn't reproducible; that is, given the 10 sample data points, it seems to run fine on everyone else's machine. I can reproduce it if I use slightly different code, as follows.

y <- read.table(tc <- textConnection("
cd cnt day year
2003-10-18   3 291 2003
2003-10-20   1 293 2003
2003-10-21   1 294 2003
2003-10-22   1 295 2003
2003-10-23   2 296 2003
2003-11-03   1 307 2003
2003-11-18   3 322 2003
2003-11-20   2 324 2003
2003-11-21   6 325 2003
2003-11-24   1 328 2003"), header = TRUE); close(tc)
y$cd <- as.Date(y$cd, format = "%Y-%m-%d")

q <- ggplot(y, aes(y$day, y$cd, fill=y$cnt)) + geom_bar(width = 0.9, position = "fill") + coord_polar(theta="x")
q

Did you actually include cd as an aesthetic in your plot?


A couple of minor style points:

You don't need to declare y$ for each of the variable names in the aes() call. ggplot is smart enough to look inside the data frame you provided in the first argument.

Best not call your variable q, since that is a function name already.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号