【发布时间】: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 <- factor(Vocab$vocabulary),那么你的色标应该会很好地显示出来。