ggplot中的十六进制颜色未按预期着色 [英] Hexadecimal colors in ggplot not colored as expected
问题描述
编辑:
我问了一个问题,归结为:
如何获取ggplot使用十六进制颜色?
MrFick的回答非常好。
我的原始问题有一个可怕的错字(阅读评论)。而不是删除这个有缺陷的问题,我建议只是不读下面的任何内容,然后阅读接受的解决方案。感谢。
:编辑结束
c $ c> geom_segment 图层,以使其看起来像一个垂直条。
p< - p + geom_segment(data = DF_for_plotting,aes(x = value [1],xend = value [2] -0.001,y = 1,yend = 1,color =green),size = 10)
p < -p + geom_segment(data = DF_for_plotting,aes(x = value [2],xend = value [3] -0.001,y = 1,yend = 1,color =blue),size = 10)
p <-p + geom_segment(data = DF_for_plotting,aes(x = value [3],xend = value [4] -0.001,y = 1,yend = 1,color =red),size = 10)
p
这给我:
由于某种原因,当我尝试用#CC6666
替换 red
,替换 green $ cccc
替换
与
,因此,#9999CC
q <-ggplot()
q < - q + geom_segment(data = DF_for_plotting,aes(x = value [1],xend = value [2] -0.001,y = 1,yend = 1,color =#66CC99),size = 10)
q ; -q + geom_segment(data = DF_for_plotting,aes(x = value [2],xend = value [3] -0.001,y = 1,yend = 1,color =#9999CC),size = 10)
q <-q + geom_segment(data = DF_for_plotting,aes(x = value [3],xend = value [4] -0.001,y = 1,yend = 1,color =#CC6666),size = 10 )
我得到:
我不知何故需要重置颜色后每个情节?为什么我需要这样做?
(FFIW,我也在闪光工作,我正在做一组6个图形,动态地。如果这是一个与颜色定义有关的问题在一个环境中,我可能有一些额外的痛苦。)
任何帮助将不胜感激。
数据存在此处,代码如下:
库(ggplot2)
库(gridExtra)
#DF_for_plotting住在这里:https://www.dropbox.com/s/6hkc3mth9oimlk5/DF_for_plotting.csv?dl=0
p< - ggplot()
p <-p + geom_segment(data = DF_for_plotting,aes(x = value [1],xend = value [2] -0.001,y = 1,yend = 1,color =green),size = 10)
p <-p + geom_segment(data = DF_for_plotting,aes(x = value [2],xend = value [3] -0.001,y = 1,yend = 1,color =blue),size = 10 )
p <-p + geom_segment(data = DF_for_plotting,aes(x = value [3],xend = value [4] -0.001,y = 1,yend = 1,color =red),size (x = value [1],xend = value [2] -0.001,...,xs-value) y = 1,yend = 1,color =#66CC99),size = 10)
q <-p + geom_segment(data = DF_for_plotting,aes(x = value [2],xend = value [3] -0.001,y = 1,yend = 1,color =#9999CC),size = 10)
q< -p + geom_segment(data = DF_for_plotting,aes(x = value [3],xend = value [4] -0.001,y = 1,yend = 1,color =#CC6666),size = 10)
grid.arrange(p,q,ncol = 2)
如果你想在geom_segment中指定文字颜色值,你应该不包括在 aes()
中。例如使用此测试数据 DF_for_plotting< - data.frame(
variable = rep(StrpCnCor, 4),
value = c(0,50.79330935,81.127731,100)
)
你可以做
ggplot()+
geom_segment(data = DF_for_plotting,aes(x = value [ 1],xend = value [2] -0.001,y = 1,yend = 1),color =green,size = 10)+
geom_segment(data = DF_for_plotting,aes ,xend = value [3] -0.001,y = 1,yend = 1),color =blue,size = 10)+
geom_segment(data = DF_for_plotting,aes = value [4] -0.001,y = 1,yend = 1),color =red,size = 10)
b $ b
>
或使用十六进制颜色
ggplot()+
geom_segment(data = DF_for_plotting,aes (x = value [2]),xend = value [2] -0.001,y = 1,yend = 1),color =#9999CC,size = 10)+
geom_segment ,xend = value [3] -0.001,y = 1,yend = 1),color =#66CC99,size = 10)+
geom_segment(data = DF_for_plotting,aes xend = value [4] -0.001,y = 1,yend = 1),color =#CC6666,size = 10)
>
虽然因为你没有映射任何东西到颜色美学,不会提供图例。
当你把它放在 aes ()
,你不是指定一个字面值,你只是指定一个文字值来与颜色关联,如果你使用 aes(color =红色)
或 aes(color =determination)
;它只是将其视为文字字符值,并将使用它自己的颜色腭为该字符值分配颜色。您可以用 scale_fill_manual
指定自己的颜色例如
ggplot +
geom_segment(data = DF_for_plotting,aes(x = value [1],xend = value [2] -0.001,y = 1,yend = 1,color =a),,size = 10)+
geom_segment(data = DF_for_plotting,aes(x = value [2],xend = value [3] -0.001,y = 1,yend = 1,color =b),,size = 10)+
geom_segment(data = DF_for_plotting,aes(x = value [3],xend = value [4] -0.001,y = 1,yend = 1,color =c),size = 10)+
scale_color_manual(values = c(a =green,b =blue,c =red))
>
ggplot()+
geom_segment(data = DF_for_plotting,aes(x = value [1],xend = value [2] -0.001,y = 1,yend = 1,color = a,(x = value [2],xend = value [3] -0.001,y = 1,yend = 1,color = a(x =值[3],xend = value [4] -0.001,y = 1,yend = 1,color =b c),size = 10)+
scale_color_manual(values = c(a =#9999CC,b =#66CC99,c =#CC6666))
这里我称之为三个组a,b和c,但你也可以调用green,blue,red如果你想 - 它只是似乎奇怪有一个传说,告诉你什么颜色是绿色的。
EDIT:
I asked a question which boils down to this:
"How can I get ggplot to use hexadecimal colors?"
The answer by MrFick was excellent.
My original question had a terrible typo (read the comments). Instead of deleting this flawed question, I'd recommend just not reading anything below this and then reading the accepted solution. Thanks.
:END OF EDIT
I have plotted a few geom_segment
layers, to make what looks like a vertical bar.
p <- p + geom_segment(data=DF_for_plotting, aes(x=value[1], xend=value[2]-0.001, y=1, yend=1, colour="green"), size=10)
p <- p + geom_segment(data=DF_for_plotting, aes(x=value[2], xend=value[3]-0.001, y=1, yend=1, colour="blue"), size=10)
p <- p + geom_segment(data=DF_for_plotting, aes(x=value[3], xend=value[4]-0.001, y=1, yend=1, colour="red"), size=10)
p
This gives me:
For some reason, when I try to replace red
with "#CC6666"
, replace green
with "#9999CC"
, and replace blue
with "#66CC99"
, as such,
q <- ggplot()
q <- q + geom_segment(data=DF_for_plotting, aes(x=value[1], xend=value[2]-0.001, y=1, yend=1, colour="#66CC99"), size=10)
q <- q + geom_segment(data=DF_for_plotting, aes(x=value[2], xend=value[3]-0.001, y=1, yend=1, colour="#9999CC"), size=10)
q <- q + geom_segment(data=DF_for_plotting, aes(x=value[3], xend=value[4]-0.001, y=1, yend=1, colour="#CC6666"), size=10)
I get:
Do I somehow need to reset the colors after each plot? And why would I need to do that?
(FFIW, I'm also working in Shiny, where I'm making a set of 6 plots, dynamically. If this is an issue relating to how color is defined in an environment, I might have some extra pain.)
Any help would be appreciated.
The data lives here and the code is here:
library(ggplot2)
library(gridExtra)
#DF_for_plotting lives here: https://www.dropbox.com/s/6hkc3mth9oimlk5/DF_for_plotting.csv?dl=0
p <- ggplot()
p <- p + geom_segment(data=DF_for_plotting, aes(x=value[1], xend=value[2]-0.001, y=1, yend=1, colour="green"), size=10)
p <- p + geom_segment(data=DF_for_plotting, aes(x=value[2], xend=value[3]-0.001, y=1, yend=1, colour="blue"), size=10)
p <- p + geom_segment(data=DF_for_plotting, aes(x=value[3], xend=value[4]-0.001, y=1, yend=1, colour="red"), size=10)
q <- ggplot()
q <- p + geom_segment(data=DF_for_plotting, aes(x=value[1], xend=value[2]-0.001, y=1, yend=1, colour="#66CC99"), size=10)
q <- p + geom_segment(data=DF_for_plotting, aes(x=value[2], xend=value[3]-0.001, y=1, yend=1, colour="#9999CC"), size=10)
q <- p + geom_segment(data=DF_for_plotting, aes(x=value[3], xend=value[4]-0.001, y=1, yend=1, colour="#CC6666"), size=10)
grid.arrange(p,q, ncol=2)
解决方案 If you want to specify a literal color value in a geom_segment, you should not include it in the aes()
. For example using this test data
DF_for_plotting <- data.frame(
variable=rep("StrpCnCor",4),
value=c(0, 50.79330935, 81.127731, 100)
)
you can do
ggplot() +
geom_segment(data=DF_for_plotting, aes(x=value[1], xend=value[2]-0.001, y=1, yend=1), colour="green", size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[2], xend=value[3]-0.001, y=1, yend=1), colour="blue", size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[3], xend=value[4]-0.001, y=1, yend=1), colour="red", size=10)
or with hex colors
ggplot() +
geom_segment(data=DF_for_plotting, aes(x=value[1], xend=value[2]-0.001, y=1, yend=1), colour="#9999CC", size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[2], xend=value[3]-0.001, y=1, yend=1), colour="#66CC99", size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[3], xend=value[4]-0.001, y=1, yend=1), colour="#CC6666", size=10)
Although because you're not mapping anything to the color aesthetic, no legend will be provided.
When you put it in the aes()
, you're not specifying a literal value, you are just specifying a literal value to associate with a color it doesn't matter if you use aes(color="red")
or aes(color="determination")
; it just treats it as a literal character value and will use it's own color palate to assign a color to that character value. You can specify your own colors with scale_fill_manual
For example
ggplot() +
geom_segment(data=DF_for_plotting, aes(x=value[1], xend=value[2]-0.001, y=1, yend=1, colour="a"), , size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[2], xend=value[3]-0.001, y=1, yend=1, colour="b"), , size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[3], xend=value[4]-0.001, y=1, yend=1, colour="c"), size=10) +
scale_color_manual(values=c(a="green",b="blue",c="red"))
ggplot() +
geom_segment(data=DF_for_plotting, aes(x=value[1], xend=value[2]-0.001, y=1, yend=1, colour="a"), , size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[2], xend=value[3]-0.001, y=1, yend=1, colour="b"), , size=10) +
geom_segment(data=DF_for_plotting, aes(x=value[3], xend=value[4]-0.001, y=1, yend=1, colour="c"), size=10) +
scale_color_manual(values=c(a="#9999CC",b="#66CC99",c="#CC6666"))
Here i called the three groups "a", "b", and "c" but you could also call then "green","blue","red" if you want -- it just seems odd to have a legend that tells you what color is green.
这篇关于ggplot中的十六进制颜色未按预期着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!