2016-05-12 32 views
0

我使用PerfomanceAnalytics包中的函數table.CalendarReturns創建了此數據框(rownames是月份,並且是年份):R - 如何有條件地爲data.frame的值着色並繪製一個圖形

tabRet 

> 
gen feb mar apr mag giu lug ago set ott nov dic System 
2004 0.0 3.5 2.9 2.0 1.1 -0.4 1.2 3.3 0.9 1.8 3.0 -0.6 20.1 
2005 1.6 2.3 -1.2 4.0 0.0 1.6 -1.4 2.4 0.7 2.9 2.9 0.4 17.3 
2006 0.8 2.7 0.3 1.4 6.2 -2.6 2.1 2.8 0.5 0.3 0.7 3.1 19.6 
2007 1.3 0.1 1.4 0.1 1.6 -1.0 1.0 1.5 -0.7 1.0 1.3 -0.7 7.0 
2008 1.4 -1.2 2.2 1.2 -0.3 -0.8 2.2 0.4 1.1 0.1 4.4 -1.3 9.7 
2009 4.8 3.2 1.6 3.5 0.7 1.7 2.1 2.2 2.5 1.9 1.5 2.8 32.4 
2010 3.5 0.5 0.4 1.3 1.8 3.8 3.7 3.0 1.1 1.2 3.9 3.4 31.2  
2011 4.3 2.1 1.6 -0.8 3.9 1.5 4.0 5.4 2.3 2.9 0.2 1.5 33.0 
2012 1.1 1.9 -0.1 2.3 1.0 3.6 1.5 0.7 0.0 1.5 1.2 0.5 16.3 
2013 0.8 2.5 1.2 1.4 0.0 1.7 2.3 1.7 0.5 0.2 1.3 0.6 15.1 
2014 0.1 0.7 0.3 -0.7 1.0 1.0 0.2 0.9 -0.7 2.3 1.4 1.4 8.2 
2015 2.3 1.0 1.1 3.1 4.5 -0.7 -0.3 2.3 2.4 0.4 -1.3 1.0 16.7 
2016 2.1 2.5 0.9 1.0 0.2 NA NA NA NA NA NA NA 7.0 

我想爲這些數字的顏色創建一個類似這樣的情節:ifelse(values< 0,'red','black')

我嘗試了addlot2plot函數,從plotrix軟件包中得到了不好的結果。 關於這個問題的任何提示?提前謝謝你們。

編輯: 我需要這樣的事,但在紅色的負數:

textplot(Hmisc::format.df(tabRet, na.blank=TRUE, numeric.dollar=FALSE, cdec=rep(1,dim(tabRet)[2])), rmar = 0.8, cmar = 1, max.cex=.9, halign = "center", valign = "center", row.valign="center", wrap.rownames=20, wrap.colnames=10, col.colnames="Darkblue",col.rownames="Darkblue", mar = c(0,0,4,0)+0.1) title(main="Calendar Monthly Returns",col.main="Darkblue", cex.main=1) 

PLOT

+0

使用ggplot(library:'ggplot2')。你可以創建一個變量'c <-ifelse(values <0,'a','b')',然後在ggplot的'color ='中使用該變量。看到這個進一步的理解:http://zevross.com/blog/2014/08/04/beautiful-plotting-in-ra-ggplot2-cheatsheet-3/ –

+0

雖然如果你使用'ggplot'你一定需要將你的月份重塑爲長格式,參見'tidyr :: gather'或'reshape2 :: melt'。 – Gregor

+0

這是否允許我繪製該表格?因爲繪製該表格我試過: textplot(Hmisc :: format.df(tabRet,na.blank = TRUE,numeric.dollar = FALSE,cdec = rep(1,dim(tabRet)[2])),rmar = 0.8,cmar = 1,max.cex = .9,halign =「center」,valign =「center」,row.valign =「center」,wrap.rownames = 20, wrap.colnames = 10,col.colnames =「Darkblue」,col.rownames =「Darkblue」,mar = c(0,0,4,0)+0.1) title(main =「Calendar Monthly Returns」,col.main =「Darkblue」,cex.main = 1) –

回答

1

只需在col.rownames="Darkblue",之後加col.data=ifelse(tabRet<0,'red','black'),即可。

+0

非常感謝你,那就是我一直在尋找的東西! –

0

下面是使用geom_tile{ggplot2}有重複的例子,一個解決方案:

# load libraries 
    library(ggplot2) 
    library(ggthemes) 
    library(data.table) 
    library(PerformanceAnalytics) 

# load data 
    data(managers) 
    df <- as.data.frame(t(table.CalendarReturns(managers[,c(1,7,8)]))) 

# Convert row names into first column 
    df <- setDT(df, keep.rownames = TRUE)[] 
    setnames(df, "rn", "month") 

# reshape your data 
    df2 <- melt(df, id.var = "month") 

# Plot 
ggplot(df2, aes(x=month, y=variable)) + 
     geom_tile(fill= "white", color = "white") + 
     geom_text(aes(label=value, color= value < 0)) + 
     scale_color_manual(guide=FALSE, values=c("red", "black")) + 
     theme_pander() + 
     theme(axis.text = element_text(face = "bold")) + 
     ggtitle("Calendar Monthly Returns") 

enter image description here

您也可以選擇填充拼貼而不是文本。

ggplot(df2) + 
    geom_tile(aes(x=month , y=variable, fill= value < 0), color = "gray70", alpha = 0.7) + 
    scale_fill_manual(guide=FALSE, values=c("red", "black")) + 
    theme_pander() 

enter image description here

在任何情況下,這個答案提供了ggplot有條件的顏色的一般方法。

ggplot(mtcars, aes(wt, mpg)) + 
    geom_point(aes(color= ifelse(mpg > 20, "A", "B"))) + 
    scale_color_manual(guide=FALSE, values=c("red", "black")) 

enter image description here

你也可以做到這一點使用base

plot(mtcars$wt, mtcars$mpg, 
    col=ifelse(mtcars$mpg > 20 ,"red", "black")) 
+0

不完全,我編輯了我的原始答案與更多細節。 –

0

如果這是你在找什麼:

require(tidyr) 
require(ggplot2) 
d<-data.table(gen=seq(2004,2016,1),Jan=round(rnorm(13)),Feb=round(rnorm(13)),Mar=round(rnorm(13))) 
gd<-gather(d,key=gen) 
gd$col<-ifelse(gd$value<0.5,"1","0") 
ggplot(data=gd,aes(x=gen,y=value,color=col))+geom_tile(aes(fill=col))+ 
    scale_fill_manual(values=c("red","black")) 

enter image description here

+0

不完全,我編輯了我的原始答案與更多細節。 –