don't show the middle line in geom_boxplot

70 views
Skip to first unread message

Sherlock Su

unread,
Mar 17, 2017, 10:41:54 AM3/17/17
to ggplot2
hi,all 

Newaday, when i try to plot Candlestick Charts by ggplot, something error report :


sz <- org_price        # load dataset 
sz$id<-1:22
sz$candleLower<-pmin(sz$OPEN,sz$CLOSE)
sz$candleUpper<-pmax(sz$OPEN,sz$CLOSE)
sz$candleMiddle <- NA
sz$color <- "red"
sz$color[sz$CLOSE<sz$OPEN] = "green"
g1<-ggplot(sz,aes(x=id))+geom_boxplot(stat='identity',alpha=.8, aes(lower=candleLower, middle=candleMiddle, upper=candleUpper, ymin=low, ymax=high,group=id, fill=color))+
  scale_fill_manual(values=c('green','red'))+guides(fill=FALSE)

Error: Discrete value supplied to continuous scale


 ggplot(sz,aes(x=as.numeric(id)))+geom_boxplot(stat='identity',alpha=.8,aes(lower=candleLower, upper=candleUpper,ymin=LOW, ymax=HIGH,group=id, fill=color))+  scale_fill_manual(values=c('green','red'))+guides(fill=FALSE)

Error: geom_boxplot requires the following missing aesthetics: middle

sz$mean<- (sz$OPEN+sz$CLOSE)/2


ggplot(sz,aes(x=as.numeric(id)))+geom_boxplot(stat='identity',alpha=.8,aes(lower=candleLower, upper=candleUpper,middle=NULL,ymin=LOW, ymax=HIGH,group=id, fill=color))+ scale_fill_manual(values=c('green','red'))+guides(fill=FALSE)

this time ggplot() works,but not the picture I want.

what should I do if i want draw the picture exactly the same as the Attachment show?

could  you give me  some advice? thank you in advance!
Rplot.png

Sherlock Su

unread,
Mar 17, 2017, 10:45:14 AM3/17/17
to ggplot2


在 2017年3月17日星期五 UTC+8下午10:41:54,Sherlock Su写道:
a.png

Brandon Hurr

unread,
Mar 17, 2017, 10:49:25 AM3/17/17
to Sherlock Su, ggplot2
Yiji, 

Could you supply a dataset that goes with your code. 

It's hard to diagnose without that. 

Use dput(sz) in your workspace and paste the output in your reply. 

B

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sherlock Su

unread,
Mar 17, 2017, 8:33:51 PM3/17/17
to ggplot2, yijic...@gmail.com
dput(sz)

structure(list(DATETIME = structure(c(17212, 17213, 17214, 17217, 
17218, 17219, 17220, 17221, 17224, 17225, 17226, 17227, 17228, 
17231, 17232, 17233, 17234, 17235, 17238, 17239, 17240, 17241, 
17242), class = "Date"), OPEN = c(42.99, 42.5, 47.04, 44.28, 
43.58, 43.92, 43.43, 43.3, 44.26, 42.73, 43.68, 44.11, 44.55, 
45.68, 50.99, 49.1, 52.16, 53.88, 55.68, 56.1, 59.11, 63.2, 53.2
), HIGH = c(44.95, 47.04, 49.56, 45.68, 44.8, 44.3, 43.9, 44.87, 
44.26, 43.81, 44.37, 45.55, 46, 50.26, 53.49, 54.49, 57, 58.96, 
57.66, 60.58, 65.9, 64.99, 55), LOW = c(42.52, 41.6, 46.72, 43.1, 
43.58, 43.07, 42.26, 43, 42.9, 42.73, 43.35, 43.83, 43.26, 45.2, 
49.63, 49.1, 51.42, 53.5, 54.51, 55.79, 59.11, 58.5, 52.65), 
    CLOSE = c(42.76, 47.04, 47.1, 43.56, 43.93, 43.64, 43.58, 
    44.36, 43.05, 43.72, 44, 44.92, 45.69, 50.26, 49.75, 52.88, 
    56.3, 56.82, 55.78, 60.35, 65, 58.5, 52.65), id = 1:23, candleLower = c(42.76, 
    42.5, 47.04, 43.56, 43.58, 43.64, 43.43, 43.3, 43.05, 42.73, 
    43.68, 44.11, 44.55, 45.68, 49.75, 49.1, 52.16, 53.88, 55.68, 
    56.1, 59.11, 58.5, 52.65), candleUpper = c(42.99, 47.04, 
    47.1, 44.28, 43.93, 43.92, 43.58, 44.36, 44.26, 43.72, 44, 
    44.92, 45.69, 50.26, 50.99, 52.88, 56.3, 56.82, 55.78, 60.35, 
    65, 63.2, 53.2), candleMiddle = c(NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA), color = c("green", "red", "red", "green", "red", 
    "green", "red", "red", "green", "red", "red", "red", "red", 
    "red", "green", "red", "red", "red", "red", "red", "red", 
    "green", "green"), mean = c(42.875, 44.77, 47.07, 43.92, 
    43.755, 43.78, 43.505, 43.83, 43.655, 43.225, 43.84, 44.515, 
    45.12, 47.97, 50.37, 50.99, 54.23, 55.35, 55.73, 58.225, 
    62.055, 60.85, 52.925)), .Names = c("DATETIME", "OPEN", "HIGH", 
"LOW", "CLOSE", "id", "candleLower", "candleUpper", "candleMiddle", 
"color", "mean"), row.names = c(NA, -23L), class = "data.frame")

在 2017年3月17日星期五 UTC+8下午10:49:25,Brandon Hurr写道:
To unsubscribe: email ggplot2+u...@googlegroups.com

More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages