【问题标题】:Why is the color palette only applying one color to all bars?为什么调色板只对所有条应用一种颜色?
【发布时间】:2021-09-14 19:27:59
【问题描述】:

我正在使用 ggplot2 创建条形图,并希望使用 RColorBrewer 调色板来填充图表的各个条形。出于某种原因,调色板仅将一种颜色应用于图形的所有条形。这是我到目前为止的代码:

prop_race_2018_plot <- ggplot(race_2018) +
geom_bar(mapping = aes(x = Race, y = X2018_Percentage, fill = "X2018_Percentage"),
       stat = "identity") +
labs(
title = "Patient Enrollment by Race, 2018",
x = "Race", # x-axis label
y = "Percentage of Patients Enrolled") +
scale_fill_brewer(palette = "Set2") +
scale_x_discrete(labels = c("American Indian/Alaska Native", "Asian", "Black",
                          "Native Hawaiian/Pacific Islander", "Other Race", 
                          "Unknown Race", "White")) +
theme(legend.position = "none")

【问题讨论】:

标签: r ggplot2 colors geom-bar palette


【解决方案1】:

fill= 变量周围删除 " 引号。

另外,你可能真的想要fill=Race

gplot(race_2018) +
    geom_bar(mapping = aes(x = Race, y = X2018_Percentage, fill = Race), stat = "identity")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-31
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-20
    相关资源
    最近更新 更多