【问题标题】:Looking to make my ggplot different colors?想让我的 ggplot 有不同的颜色?
【发布时间】:2018-10-02 21:36:11
【问题描述】:
Teeth <- caries$after - caries$before
anova <- aov(Teeth~inst * treat, data=caries)
summary(anova)

ggplot(data = caries, aes(x=inst, y=Teeth, group=inst)) + 
       geom_boxplot(colour = "black", fill = "dodgerblue")

这是我目前的代码 - caries 是导入的数据集。

我正在尝试使三个不同的盒子颜色不同,而现在它们只有深蓝色。

数据

caries <- structure(list(subject = 1:69, inst = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), 
    before = c(7L, 20L, 21L, 1L, 3L, 20L, 9L, 2L, 11L, 15L, 7L, 
    17L, 9L, 1L, 3L, 10L, 13L, 3L, 4L, 4L, 15L, 6L, 4L, 18L, 
    11L, 9L, 4L, 5L, 11L, 4L, 4L, 7L, 0L, 3L, 0L, 8L, 2L, 13L, 
    9L, 15L, 13L, 2L, 9L, 4L, 10L, 7L, 14L, 7L, 3L, 9L, 8L, 19L, 
    10L, 10L, 7L, 13L, 5L, 1L, 8L, 4L, 4L, 14L, 8L, 3L, 11L, 
    16L, 8L, 0L, 3L), after = c(11L, 24L, 25L, 2L, 7L, 23L, 13L, 
    4L, 13L, 18L, 10L, 17L, 11L, 5L, 7L, 14L, 17L, 4L, 7L, 9L, 
    18L, 8L, 6L, 19L, 12L, 9L, 7L, 7L, 14L, 6L, 4L, 7L, 4L, 3L, 
    1L, 8L, 4L, 18L, 12L, 18L, 17L, 5L, 12L, 6L, 14L, 11L, 15L, 
    10L, 6L, 12L, 10L, 19L, 13L, 12L, 11L, 12L, 8L, 3L, 9L, 5L, 
    7L, 14L, 10L, 5L, 12L, 18L, 8L, 1L, 4L), treat = structure(c(3L, 
    3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 
    3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
    1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("APF", "SF", 
    "W"), class = "factor")), .Names = c("subject", "inst", "before", 
"after", "treat"), class = "data.frame", row.names = c(NA, -69L
))

https://pastebin.com/MeRw69ky

【问题讨论】:

  • 要创建MCVE,您需要提供数据(例如,使用dput)或提供使用公开数据集的示例。
  • 您好!谢谢!我是该网站的新手,这是我的第一篇文章,我正在尝试弄清楚如何使用 dput
  • 欢迎来到 SO。请浏览网站方向和帮助部分。另外,请阅读this。该网站普遍认为,在发布问题之前,您应该自己进行合理数量的研究。有很多资源可以回答你的问题,一个简单的谷歌搜索“ggplot 中的颜色”可能就足够了。
  • 嗨!感谢您提供这些资源。我已经尝试了 5 种代码变体并不断收到错误
  • ggplot(data = caries, aes(x=inst, y=Teeth, group=inst)) + geom_boxplot(bp + scale_fill_manual(values=c("red", "dodgerblue", "green ")) 错误:出现意外符号:“ggplot(data = caries, aes(x=inst, y=Teeth, group=inst)) + geom_boxplot(scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9")) ggplot" > bp + scale_fill_manual(breaks = c("2", "1", "0.5"), + values=c("red", "blue", "green")) 错误:找不到对象 'bp' > ggplot(data = caries, aes(x=inst, y=Teeth, fill=inst)) + geom_boxplot(scale_fill_hue(l=40, c=35)) 错误:必须由 @ 创建映射987654327@或aes_()

标签: r ggplot2 colors


【解决方案1】:

为了让变量确定绘图中的颜色,您必须在aes 调用中将该变量映射到颜色。这是mpg 数据集的示例。我建议阅读ggplot2 docs,因为那里有大量示例,其中至少有一个与此示例基本相同。

library(ggplot2)

ggplot(mpg, aes(x = class, y = hwy, color = class)) +
    geom_boxplot()

reprex package (v0.2.0) 于 2018 年 4 月 22 日创建。

【讨论】:

【解决方案2】:

我已经用这段代码修复了它。谢谢大家的帮助!

ggplot(caries, aes(inst, Teeth, group=inst, fill = inst)) + geom_boxplot()

ggplot(caries, aes(treat, Teeth, group=treat, fill = treat)) + geom_boxplot()

【讨论】:

    猜你喜欢
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2018-10-24
    相关资源
    最近更新 更多