Hi I have a question regarding plots in R. I have data from the S&P 500 in the format: date close change 1980-01-07 109.92 3.4 I plotted the data with plot(spdata$date, log(spdata$close), type="p") Now I want to ad the colors green and red to the data frame. if the change is positive it should be green, if negative, red. Also the colors should be in the plot. I tried to do this with the ifelse function, but seem to be stuck. Can anyone help me with this? Thanks a lot! -- View this message in context: http://r.789695.n4.nabble.com/R-plotting-tp4678056.html Sent from the R help mailing list archive at Nabble.com.
On 10/11/2013 11:45 PM, Mubar wrote:> Hi > > I have a question regarding plots in R. I have data from the S&P 500 in the > format: > > date close change > 1980-01-07 109.92 3.4 > > I plotted the data with plot(spdata$date, log(spdata$close), type="p") > > Now I want to ad the colors green and red to the data frame. if the change > is positive it should be green, if negative, red. Also the colors should be > in the plot. I tried to do this with the ifelse function, but seem to be > stuck. >Hi Mubar, I think this will get you what you want: plot(spdata$date,log(spdata$close),type="p", col=(spdata$change>=0)+2) Jim
Hi This seems to work: spdata$color <- ifelse(spdata$change < 0, "red", "green") plot(spdata$date, log(spdata$close), col = spdata$color) Regards Mikkel On Friday, October 11, 2013 5:14 PM, Mubar <simon.keusen at student.unisg.ch> wrote: Hi I have a question regarding plots in R. I have data from the S&P 500 in the format: date? ? ? ? ? close? ? change 1980-01-07? 109.92? 3.4 I plotted the data with plot(spdata$date, log(spdata$close), type="p") Now I want to ad the colors green and red to the data frame. if the change is positive it should be green, if negative, red. Also the colors should be in the plot. I tried to do this with the ifelse function, but seem to be stuck. Can anyone help me with this? Thanks a lot! -- View this message in context: http://r.789695.n4.nabble.com/R-plotting-tp4678056.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.