本文介绍了使所有正值条形图与带有负值的条形图在ggplot中相同的颜色主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我昨天刚开始玩ggplot。我的负值条形图代码的工作原理如下: dtf1< - data.frame (1:10),Diff = c(-5:4)) dtf1 $ color< - 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 y = 0,color = color))+ geom_bar(stat =identity,position =identity,aes(fill = color)) 但不所以当我将相同的代码应用于只有正值的不同数据集时 dtf dtf $ color dtf $ hjust < - ifelse(dtf $ Diff> 0,1.3,-0.3) ggplot(dtf,aes(ID,Diff,label =,hjust = hjust))+ geom_text y = 0,color = color))+ geom_bar(stat =identity,position =identity,aes(fill = color)) 我发现我可以调整代码的最后一行以获得正棒的蓝色颜色, geom_bar(stat =identity,position =因此我的两个问题是: ol> 但是,颜色不是预期的: 看起来颜色可能更接近turquoise3而不是 此外,我也有兴趣了解为什么相同的代码允许正 我一直在问一个非常简单的问题。我不知道如何最好地用它来解释,因此很难找到解决方案。如果问题已经被问过,我很抱歉,我很乐意删除自己。 解决方案美学在 ggplot 中不起作用。 $ color 被视为具有两个级别的因子, firebrick1 和 steelblue ,但这些 不 ggplot 使用的颜色。它们只是颜色标度的标签。 ggplot 选择自己的颜色。如果要覆盖默认值,请添加以下行: scale_fill_manual(values = c(firebrick1 =firebrick1,steelblue = steelblue)) 与此比较: dtf1 $ color< - ifelse(dtf1 $ Diff< 0,negative,positive) ggplot(dtf1,aes =,hjust = hjust))+ geom_bar(stat =identity,position =identity,aes(fill = color))+ scale_fill_manual(values = c(positive =firebrick1 ,negative =steelblue)) 这适用于所有正面(或负面)。 dtf dtf $ color< - ifelse (dtf $ Diff dtf $ hjust< - ifelse(dtf $ Diff> 0,1.3,-0.3) ggplot ID,Diff,label =,hjust = hjust))+ geom_bar(stat =identity,position =identity,aes(fill = color))+ scale_fill_manual (positive =firebrick1,negative =steelblue)) I just started playing with ggplot yesterday. My code for the bar graph with negative values work as I have expected: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))But not so when I apply the same code to a different dataset that has only positive valuesdtf <- 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))I find that I can tweak the last line of the code to get blue colors for my positive bars,geom_bar(stat="identity",position="identity",fill="steelblue")My two questions therefore are:However, the color isn't as expected:It seems like the color may be closer to turquoise3 instead of to steelblue.Moreover, I am also interested in finding why the same code wouldallow the positive bars to have different colors.I must have been asking a very simple question. I don't know how best to phrase it and therefore have difficulty finding the solution. I apologize if the question has already been asked and I would be glad to delete myself. 解决方案 Aesthetics don't work that way in ggplot. $colour is treated as a factor with two levels, firebrick1, and steelblue, but these are not the colors ggplot uses. They are just the labels for the color scale. ggplot picks it's own colors. If you want to override the defaults, add the line:scale_fill_manual(values=c(firebrick1="firebrick1",steelblue="steelblue"))Compare to this:dtf1$colour <- ifelse(dtf1$Diff < 0, "negative","positive")ggplot(dtf1,aes(ID,Diff,label="",hjust=hjust))+ geom_bar(stat="identity",position="identity",aes(fill = colour))+ scale_fill_manual(values=c(positive="firebrick1",negative="steelblue"))This works with all positive (or negative).dtf <- data.frame(ID = c(1:10),Diff = rnorm(10,3))dtf$colour <- ifelse(dtf$Diff < 0,"negative","positive")dtf$hjust <- ifelse(dtf$Diff > 0, 1.3, -0.3)ggplot(dtf,aes(ID,Diff,label="",hjust=hjust))+ geom_bar(stat="identity",position="identity",aes(fill = colour))+ scale_fill_manual(values=c(positive="firebrick1",negative="steelblue")) 这篇关于使所有正值条形图与带有负值的条形图在ggplot中相同的颜色主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 16:15