I can not find a solution for a rather simple plot, like this one
http://www.cpc.noaa.gov/products/precip/CWlink/pna/month_nao_index.shtml.
The idea is to use geom_ribbon with different colour for values below
with y<0 and y>0.
url <- 'http://www.cpc.noaa.gov/products/precip/CWlink/pna/norm.nao.monthly.b5001.current.ascii'
file <- tempfile()
download.file(url,file)
nao <- read.table(file, header = F)
names(nao) <- c('Year', 'Month', 'NAO')
nao$Date <- as.Date(paste(nao$Year, nao$Month, '15'), format = "%Y %m %d")
library(ggplot2)
ggplot(nao, aes(Date, ymin = ifelse(NAO>0, 0, NAO))) +
geom_ribbon(aes(ymax = ifelse(NAO>0, NAO, 0),
colour = ifelse(NAO>0, 'red', 'blue'),
fill = ifelse(NAO>0, 'red', 'blue')))
This seems to split the data into two parts (NAO>0 and NAO<0) which isb
not what i want.
ANy better ideas?
thanks and best regard,
Stefan
> sessionInfo()
R version 2.12.0 (2010-10-15)
Platform: i486-pc-linux-gnu (32-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] grid graphics grDevices utils datasets stats methods
[8] base
other attached packages:
[1] ggplot2_0.8.8 proto_0.3-8 reshape_0.8.3
[4] plyr_1.2.1 ProjectTemplate_0.1-3 foreign_0.8-41
[7] yaml_1.1.0 testthat_0.3
loaded via a namespace (and not attached):
[1] digest_0.4.2 evaluate_0.3 stringr_0.4
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
thanks, but your plot is the same as mine, besides the colours.
Like mine, your plot splits the data into to parts and plots both.
Therefore, years with positive and negative NAO values occure, which is
just wrong.
regards,
stefan
> > To unsubscribe: email ggplot2+u...@googlegroups.com<ggplot2%2Bunsu...@googlegroups.com>
> > More options: http://groups.google.com/group/ggplot2
Is this the intended picture though? Looks like positives and negatives with neighboring dates will need displaced somehow or else you have this overlap as below:
![]()
Adam Loveland
216-689-5409
1Key
Dennis Murphy <djm...@gmail.com>
Sent by: ggp...@googlegroups.com12/17/2010 03:31 PM
Toggp...@googlegroups.com ccsmu <s...@z107.de> SubjectRe: geom_ribbon and when below or above y=0
More options: http://groups.google.com/group/ggplot2[attachment "nao.png" deleted by Adam Loveland/GL/KB/KeyCorp]
This communication may contain privileged and/or confidential information. It is intended solely for the use of the addressee. If you are not the intended recipient, you are strictly prohibited from disclosing, copying, distributing or using any of this information. If you received this communication in error, please contact the sender immediately and destroy the material in its entirety, whether electronic or hard copy. This communication may contain nonpublic personal information about consumers subject to the restrictions of the Gramm-Leach-Bliley Act. You may not directly or indirectly reuse or redisclose such information for any purpose other than to provide the services for which you are receiving the information. 127 Public Square, Cleveland, OH 44114
If you prefer not to receive future e-mail offers for products or services from Key
send an e-mail to mailto:DNERe...@key.com with 'No Promotional E-mails' in the
SUBJECT line.
no sorry, still something is wrong, which gets visible, when a small part
is the data is plotted
nao$pos <- with(nao, pmax(0, NAO))
nao$neg <- with(nao, pmin(0, NAO))
nao = subset(nao, Date>as.Date('2009-01-01'))
g <- ggplot(nao, aes(x = Date))
g + geom_ribbon(aes(ymin = 0, ymax = pos), fill = 'red') +
geom_ribbon(aes(ymin = neg, ymax = 0), fill = 'blue')
# compared to
g + geom_line(aes(y=NAO))
I guess it would be easy correct, when the points where y=0 where
included in the dataset. But is this the only way to solve this problem?
regards,
stefan
Dennis Murphy <djm...@gmail.com>
Sent by: ggp...@googlegroups.com |
12/17/2010 04:44 PM |
|
|
More options: http://groups.google.com/group/ggplot2[attachment "nao.pdf" deleted by Adam Loveland/GL/KB/KeyCorp]
Adam Loveland/GL/KB/KeyCorp
12/17/2010 05:01 PM |
|
On Fri, Dec 17, 2010 at 02:37:31PM -0800, Dennis Murphy wrote:
> Hi:
>
> When you use geom_ribbon(), there are going to be regions of overlap if you
> zoom in far enough. The reason the blues and reds overlap in the subset is
> because there is either a single negative value in a region of positive
> values or vice versa, and geom_ribbon() needs to plot some area- you can see
> that it goes +/- 0.5 units to plot a triangle when the value is a singleton.
Yes, that was my problem, I thought there could be a simple approach to
solve, e.g. by determining, where y=0 and includes these dates in the
data.
Thank you for your suggestion of the segment plot, I think I will use
this solution.
best regards,
Stefan
Hi Dennis,
Yes, that was my problem, I thought there could be a simple approach to
On Fri, Dec 17, 2010 at 02:37:31PM -0800, Dennis Murphy wrote:
> Hi:
>
> When you use geom_ribbon(), there are going to be regions of overlap if you
> zoom in far enough. The reason the blues and reds overlap in the subset is
> because there is either a single negative value in a region of positive
> values or vice versa, and geom_ribbon() needs to plot some area- you can see
> that it goes +/- 0.5 units to plot a triangle when the value is a singleton.
solve, e.g. by determining, where y=0 and includes these dates in the
data.
d <- data.frame(x=rnorm(30,0,5), y=rnorm(30))
d <- d[order(d$x),] # sort along x
rx <- do.call("rbind",
sapply(1:(nrow(d)-1), function(i){
f <- lm(x~y, d[i:(i+1),])
if (f$qr$rank < 2) return(NULL)
r <- predict(f, newdata=data.frame(y=0))
if(d[i,]$x < r & r < d[i+1,]$x)
return(data.frame(x=r,y=0))
else return(NULL)
})
)
d2 <- rbind(d,rx)
ggplot(d2,aes(x,y)) + geom_point() + geom_area(data=subset(d2, y<=0),
fill="red") + geom_area(data=subset(d2, y>=0), fill="blue")
--
Kohske Takahashi <takahash...@gmail.com>
Research Center for Advanced Science and Technology,
The University of Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
thank you, perfect solution!
best regards,
Stefan