编辑:
我问了一个问题归结为:
"我如何让ggplot使用十六进制颜色?"
MrFick的答案非常好.
我原来的问题有一个可怕的错字(阅读评论).我不建议删除这个有缺陷的问题,而是建议不要阅读下面的任何内容,然后阅读已接受的解决方案.谢谢.
:编辑结束
我已经绘制了几个geom_segment图层,以使看起来像垂直条.
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
Run Code Online (Sandbox Code Playgroud)
这给了我:

出于某种原因,当我尝试更换red用"#CC6666",更换green用"#9999CC",并更换blue有"#66CC99",正因为如此,
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)
Run Code Online (Sandbox Code Playgroud)
我明白了:

我是否需要在每个情节后重置颜色?为什么我需要这样做?
(FFIW,我也在Shiny工作,我正在动态制作一组6个图.如果这是一个与环境中的颜色定义有关的问题,我可能会有一些额外的痛苦.)
任何帮助,将不胜感激.
数据存在这里,代码在这里:
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)
Run Code Online (Sandbox Code Playgroud)
如果要在geom_segment中指定文字颜色值,则不应将其包含在aes().例如,使用此测试数据
DF_for_plotting <- data.frame(
variable=rep("StrpCnCor",4),
value=c(0, 50.79330935, 81.127731, 100)
)
Run Code Online (Sandbox Code Playgroud)
你可以做
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)
Run Code Online (Sandbox Code Playgroud)

或者用十六进制颜色
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)
Run Code Online (Sandbox Code Playgroud)

虽然因为您没有将任何内容映射到颜色美学,但不会提供任何图例.
当你把它放入时aes(),你没有指定文字值,你只是指定一个文字值来与一个颜色相关联,如果你使用aes(color="red")或者无关紧要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, 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"))
Run Code Online (Sandbox Code Playgroud)

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"))
Run Code Online (Sandbox Code Playgroud)

在这里我称三个组为"a","b"和"c",但如果你愿意的话,你也可以称之为"绿色","蓝色","红色" - 有一个传说告诉它似乎很奇怪你是什么颜色的绿色.
| 归档时间: |
|
| 查看次数: |
2403 次 |
| 最近记录: |