【问题标题】:R - How to conditionally color the values of a data.frame and make a plotR - 如何有条件地为 data.frame 的值着色并制作绘图
【发布时间】:2016-09-08 14:11:28
【问题描述】:

我使用 PerfomanceAnalytics 包中的函数 table.CalendarReturns 创建了这个数据框(行名是年份,列名是月份):

tabRet

> 
 gen  feb  mar  apr  may  jun  jul  aug sep  oct nov  dec 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')

我尝试使用 plotrix 包中的 addtable2plot 函数,但结果不佳。 关于这个问题的任何提示?提前谢谢你们。

编辑: 我需要这样的东西,但红色的负数:

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

【问题讨论】:

  • 使用 ggplot(库:ggplot2)。您可以创建一个变量c<-ifelse(values< 0,'a','b'),然后在ggplot 中的color= 中使用该变量。请参阅此以进一步了解:zevross.com/blog/2014/08/04/…
  • 虽然如果你使用ggplot,你肯定需要将你的月份改成长格式,见tidyr::gatherreshape2::melt
  • 这是否允许我以这种形式绘制此表?因为为了绘制该表,我尝试了: 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="日历月报表",col.main="Darkblue", cex.main=1)

标签: r colors dataframe performanceanalytics


【解决方案1】:

只需在col.rownames="Darkblue", 之后添加col.data=ifelse(tabRet<0,'red','black'), 到您的代码中

【讨论】:

  • 非常感谢,这正是我想要的!
【解决方案2】:

这是一个使用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")

您也可以选择填充图块而不是文本。

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()

无论如何,这个答案提供了ggplot 中条件颜色的一般方法。

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

您也可以使用base

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

【讨论】:

  • 不完全是,我用更多细节编辑了我的原始答案。
【解决方案3】:

如果这是您正在寻找的:

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"))

【讨论】:

  • 不完全是,我用更多细节编辑了我的原始答案。
【解决方案4】:

不知道有没有找到解决办法,希望这段代码对你有帮助 这段代码接收一个data.frame并将其转换为条件格式,单元格的值显示在列中

[数据 i 一个 data.frame][1] [1]:https://i.stack.imgur.com/jETa8.png

pmensual <- readXL("D:precipitacion mensual 
2021/Base de Datos Precipitación mensual .xls",
                   rownames=FALSE, header=TRUE, na="", sheet="Preci_mes", 
                   stringsAsFactors=TRUE)

nba.m <- melt(pmensual)
nba.s <- ddply(nba.m, .(variable), transform)
ggplot(nba.s, aes(variable, Nombre)) + 
   geom_tile(aes(fill = value), colour = "white") +
   #scale_colour_gradientn(colours = c("blue", "green", "red"))+
   scale_fill_gradient2(low = "white", high = "red", mid = "blue", midpoint = 400) +
   geom_text(aes(label = round(value, 1)), size= 3.5, vjust = 0.4)+
   scale_x_discrete("Mes", expand = c(0, 0)) + 
   scale_y_discrete("Estaciones", expand = c(0, 0)) + 
   theme( legend.position="bottom", legend.title=element_text(), panel.background = 
   element_rect(fill = "white", colour = "gray"),
          panel.grid.major = element_line(colour = "gray"))+
   
   labs(title = "Mapa de Calor Precipitaciones en la zona de Estudio",
        subtitle = "Precipitacion Acumulada al mes" , fill = "Precipitacion mm" )

[结果][2] [2]:https://i.stack.imgur.com/FVYV4.png

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2023-04-03
    • 1970-01-01
    • 2014-07-30
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    相关资源
    最近更新 更多