【发布时间】:2014-10-04 15:43:39
【问题描述】:
我昨天刚开始玩 ggplot。我的负值条形图代码按预期工作:
dtf1 <- data.frame(ID = c(1:10),Diff = c(-5:4))
dtf1$colour <- ifelse(dtf1$Diff < 0, "firebrick1","steelblue")
dtf1$hjust <- ifelse(dtf1$Diff > 0, 1.3, -0.3)
ggplot(dtf1,aes(ID,Diff,label="",hjust=hjust))+
geom_text(aes(y=0,colour=colour))+
geom_bar(stat="identity",position="identity",aes(fill = colour))
但当我将相同的代码应用于只有正值的不同数据集时,情况并非如此
dtf <- data.frame(ID = c(1:10),Diff = rnorm(10,3))
dtf$colour <- ifelse(dtf$Diff < 0, "firebrick1","steelblue")
dtf$hjust <- ifelse(dtf$Diff > 0, 1.3, -0.3)
ggplot(dtf,aes(ID,Diff,label="",hjust=hjust))+
geom_text(aes(y=0,colour=colour))+
geom_bar(stat="identity",position="identity",aes(fill = colour))
我发现我可以调整代码的最后一行来为我的正条获得蓝色,
geom_bar(stat="identity",position="identity",fill="steelblue")
因此,我的两个问题是:
- 但是,颜色不符合预期:
看起来颜色可能更接近绿松石3而不是钢蓝。
- 此外,我还想知道为什么相同的代码会 允许正面条具有不同的颜色。
我一定是在问一个非常简单的问题。我不知道如何最好地表达它,因此很难找到解决方案。如果问题已经被问过,我很抱歉,我很乐意删除自己。
【问题讨论】: