【问题标题】:In RStudio, scale_fill_brewer() creates grey bars as default bar graph, not a range of blues在 RStudio 中, scale_fill_brewer() 创建灰色条作为默认条形图,而不是一系列蓝色
【发布时间】:2018-07-26 22:09:47
【问题描述】:

我正在尝试使用 RStudio 中 car 包中的 Vocab 数据集为带有 scale_fill_brewer() 的条形图着色。

library(tidyverse)
library(car)
library(RColorBrewer)

# An ordinal dataset
ggplot(Vocab, aes(x = education, fill = vocabulary)) +
 geom_bar(position = "fill") +
 # Use the default brewed color palette
 scale_fill_brewer()

通过 DataCamp 使用在线 R 会话,我能够生成: default RColorBrewer palette is "Blues"

由于scale_fill_brewer() 调用的默认RColorBrewer 调色板是“Blues”,我希望在我的桌面上看到上面的图。但是,当我将上述代码传输到 Windows 10 上的 RStudio 时,ggplot2 会生成:grey bars instead of blue gradation

我试图通过以下方式纠正情节: # 从 RColorBrewer 包中定义一组蓝色 蓝调

# Make a color range using colorRampPalette()
blue_range <- colorRampPalette(blues)

# Use blue_range to adjust the color of the bars
ggplot(Vocab, aes(x = education, fill = vocabulary)) +
  geom_bar(position = "fill") +
  scale_fill_manual(values = blue_range(11))

此代码块应生成条形图that looks like this。但是,事实并非如此。所有的酒吧仍然是灰色的。

我不明白为什么此代码适用于在线 R 会话,但不适用于我自己的个人 RStudio 会话。帮忙?

【问题讨论】:

  • Vocab$vocabulary 是数字,但您正在尝试使用仅适用于因子的离散填充比例。在绘图前运行Vocab$vocabulary &lt;- factor(Vocab$vocabulary),那么你的色标应该会很好地显示出来。

标签: r rstudio


【解决方案1】:

使用fill = factor(vocabulary)

library(tidyverse)
library(car)
library(RColorBrewer)

blues <- brewer.pal(9, "Blues")
blue_range <- colorRampPalette(blues)

ggplot(Vocab, aes(x = education, fill = factor(vocabulary))) +
  geom_bar(position = "fill") +
  scale_fill_manual(values = blue_range(11))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 2013-06-20
    相关资源
    最近更新 更多