【问题标题】:How can I fill and color based on a variable如何根据变量填充和着色
【发布时间】:2017-03-07 08:45:16
【问题描述】:

在我的 df 中,我有一列是颜色名称,即红色、绿色...... 我想用这些颜色来填充和着色。但是我无法像那样绘制它,黑色变成橙色,黄色变成粉红色......

new.df <- structure(list(Expr = c(1, 0.75, 0.5, 0.25, 0, 1, 0.75, 0.5, 
0.25, 0, 1, 0.75, 0.5, 0.25, 0, 1, 0.75, 0.5, 0.25, 0, 1, 0.75, 
0.5, 0.25, 0, 1, 0.75, 0.5, 0.25, 0, 1, 0.75, 0.5, 0.25, 0, 1, 
0.75, 0.5, 0.25, 0), react = c(0, 0.25, 0.5, 0.75, 1, 0, 0.25, 
0.5, 0.75, 1, 0, 0.25, 0.5, 0.75, 1, 0, 0.25, 0.5, 0.75, 1, 0, 
0.25, 0.5, 0.75, 1, 0, 0.25, 0.5, 0.75, 1, 0, 0.25, 0.5, 0.75, 
1, 0, 0.25, 0.5, 0.75, 1), variable = structure(c(1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 
4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 
8L, 8L, 8L, 8L, 8L), .Label = c("black", "blue", "brown", "green", 
"pink", "red", "turquoise", "yellow"), class = c("ordered", "factor"
)), value = c(0.230403800475059, 0.0617577197149644, 0.0843230403800475, 
0.0819477434679335, 0.0819477434679335, 0.200712589073634, 0.187648456057007, 
0.144893111638955, 0.156769596199525, 0.142517814726841, 0.0451306413301663, 
0.114014251781473, 0.125890736342043, 0.133016627078385, 0.130641330166271, 
0.328978622327791, 0.0665083135391924, 0.0902612826603325, 0.0973871733966746, 
0.0855106888361045, 0.194774346793349, 0.0605700712589074, 0.0451306413301663, 
0.0700712589073634, 0.0653206650831354, NA, 0.0617577197149644, 
0.0605700712589074, 0.0795724465558195, 0.0783847980997625, NA, 
0.336104513064133, 0.0617577197149644, 0.0819477434679335, 0.0617577197149644, 
NA, 0.111638954869359, 0.0498812351543943, 0.168646080760095, 
0.0831353919239905)), row.names = c(NA, -40L), .Names = c("Expr", 
"react", "variable", "value"), class = "data.frame")
> scal <- scale_colour_manual(name = "variable", values = new.df$variable)
>   ggplot(new.df, aes(x = value, fill = variable)) +
+     facet_wrap(~Expr+react, labeller = label_wrap_gen(multi_line = FALSE)) +
+     geom_histogram(bins = 5) + theme_bw() + scal

根据this 的回答,我尝试手动创建色标,但没有成功。

【问题讨论】:

    标签: r ggplot2 colors


    【解决方案1】:

    您只需要更改一行代码:

    scal <- scale_fill_manual(values = unique(as.character(new.df$variable)))
    

    并保持其余部分不变:

    ggplot(new.df, aes(x = value, fill = variable)) +
      facet_wrap(~Expr+react, labeller = label_wrap_gen(multi_line = FALSE)) +
      geom_histogram(bins = 5) + theme_bw() + scal
    

    得到这个输出:

    【讨论】:

    • 这与我的解决方案相同。我也只是将原始变量转换为有序因子,这样更安全(我知道大部分时间是无用的,但当我想控制如何分配美学时,我总是这样做)。
    • 好吧,别担心,这只是为了说明它。我们只应该关心的基本事情是为社区提供足够的答案;)我只是“失望”我浪费了宝贵的时间在评论部分和 SO 聊天中帮助 OP,然后 OP 决定接受等效答案,根据 SO 的规定,这是完全公平的。
    【解决方案2】:

    试试

    new.df$variable <- ordered(new.df$variable, levels = unique(new.df$variable))
    
    ggplot(new.df, aes(x = value, fill = variable)) +
    facet_wrap(~Expr+react, labeller = label_wrap_gen(multi_line = FALSE)) +
    geom_histogram(bins = 5) + theme_bw() +
    scale_fill_manual(name = "variable", 
                        values = unique(as.character(new.df$variable)))
    

    然后重新执行ggplot命令。

    Ps:你的代码有错别字,我认为:你写的

    values = new.df$variables
    

    而不是values = new.df$variable

    【讨论】:

    • 我试过了,但没有用(即使纠正了错字)。为什么它的顺序会影响 ggplot2 使用的颜色?
    • scale_colour_manual(name = "variable", values = unique(new.df$variable))
    • 你能dput()你的数据或样本吗?如果不能在数据上进行尝试,很难想出答案
    • 我已经发布了我的 new.df 的头部,但如果你愿意,我可以发布整个数据
    • 更好.. 或者至少是包含所有颜色的随机样本。使用dput(new.df),然后复制粘贴到这里
    猜你喜欢
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 2015-06-25
    相关资源
    最近更新 更多