我希望能够从存储在数据框中的值设置ggplot填充颜色.下面的代码几乎是我试图做的,除了不仅仅使用fill = MyColor,我希望代码在MyColor字段中实际使用RRGGBB十六进制值.
df = data.frame(Animals = c("Dog", "Cat", "Horse", "Giraffe"),
Number = c(88, 11, 107, 59),
MyColor = c("FFFFFF", "D9FFFF", "CC80FF", "FFB5B5"))
p <- ggplot(df)
p <- p + aes(x = Animals, y = Number, fill = MyColor)
p <- p + geom_bar(stat = 'identity')
print(p)
Run Code Online (Sandbox Code Playgroud)
谢谢,
保罗
我有一个按预期工作的函数,直到我将其子集化.plotCalendar()函数是我使用ggplot2和facet在Calendar热图上的尝试.y轴顺序很重要,因为它是"WeekOfMonth" - 当订单被颠倒时,数据viz看起来不像日历.
代码如下,首先是调用代码,然后是生成一些数据的函数 - generateData(),然后是plot函数 - plotCalendar()
当我使用df作为数据时,代码按预期工作,但是当我使用df2(子集化数据)时,WeekOfMonth的顺序沿y轴反转.
library(ggplot2)
library(ProgGUIinR)
library(chron)
df <- generateData()
plotCalendar(df, dateFieldName = "dates", numericFieldName = "counts", yLab = "Month of Year")
df2 <- df[df$filterField == 42, ]
plotCalendar(df2, dateFieldName = "dates", numericFieldName = "counts", yLab = "Month of Year")
Run Code Online (Sandbox Code Playgroud)
generateData <- function()
{
set.seed(42)
dates <- seq(as.Date("2012/01/01"), as.Date("2012/6/30"), by = "1 day")
counts <- 1:length(dates)
filterField <- sample(1:42,length(dates),replace=T)
df <- data.frame(dates, counts, filterField)
return(df)
}
plotCalendar <- function(data, dateFieldName, numericFieldName, title = "Title", …Run Code Online (Sandbox Code Playgroud)