【问题标题】:Matching axis.text labels to colors contained in data frame variable in ggplot将axis.text标签与ggplot中数据框变量中包含的颜色匹配
【发布时间】:2017-02-01 20:08:09
【问题描述】:

我想创建一个堆叠条形图,其中我的axis.text 从数据框中的变量中获取颜色值,该变量还提供条形的fill 颜色。这非常重要,因为最终视觉效果的消费者将查看一系列这些条形图,因此我需要确保每种产品类型的颜色一致,即使数量值(以及顺序)会有所不同。下面是我能得到的最接近的。

# My data sample
df <- data.frame(x=1:4, Type = c("Metals", "Foodstuff", "Textiles", "Machinery"), myColour = c('blue', 'red', 'green', 'orange'), Amount = c(75, 50, 25, 5))

# Create factor to order by amount value
df$Type <- factor(df$Type, levels = df[order(df$Amount), "Type"])

# MAKE BAR
gg1 <- ggplot(df, aes(Type, Amount, fill = Type, color = myColour)) +
  geom_bar(stat = 'identity', position = 'dodge', show.legend = FALSE, width = .85, colour = 'lightgrey', fill = df$myColour) + 
  #ggtitle("Exports Profile (%)") +
  labs(x = NULL, y = NULL) +
  scale_y_continuous(breaks = waiver(), limits = c(0,100)) +
  theme(#plot.title = element_text(family= 'sans', color = 'black', size = 28), 
    #axis.title = element_text(family= 'sans', color = 'black', size = 24), 
    axis.text.y = element_text(colour = df$myColour, size = 18, face = 'bold'),
    axis.ticks.y = element_blank(),
    axis.text.x = element_text(colour = 'black', size = 16),
    axis.ticks.x = element_line(colour = 'grey60'),
    axis.ticks.length = unit(3, "mm"),
    axis.line = element_line(NULL),
    plot.background = element_rect(fill = NULL),
    panel.background = element_rect(fill = 'white', colour = 'white'),
    panel.grid.major.x = element_line(colour = 'grey60', linetype = 'dashed'),
    panel.grid.major.y = element_line(colour = 'grey60', linetype = 'dashed'),
    #panel.margin = unit(c(0,0,0,0), "mm"),
    aspect.ratio = (600/450)) + 
  coord_flip()
gg1

产生:

【问题讨论】:

    标签: r ggplot2 colors geom-bar


    【解决方案1】:

    您的因子水平未与因子顺序的变化对应。

    请注意,我对您的 df 进行了更改,因此在重新排序时确实会发生更改,更改位于 Amount 列中。

    df <- data.frame(x=1:4, Type = c("Metals", "Foodstuff", "Textiles", "Machinery"), 
            myColour = c('blue', 'red', 'green', 'orange'), Amount = c(50, 75, 25, 5))
    

    帮自己一个忙,加载 tidyverse

    library(tidyverse)
    

    然后使用theme_set

    theme_set(theme_classic()+
              theme(panel.grid.major.x = element_line(colour = 'grey60', linetype = 'dashed'),
                    panel.grid.major.y = element_line(colour = 'grey60', linetype = 'dashed'),
                    axis.ticks.y = element_blank(),
                    axis.text.x = element_text(colour = 'black', size = 16),
                    axis.ticks.x = element_line(colour = 'grey60'),
                    axis.ticks.length = unit(3, "mm"),
                    aspect.ratio = (600/450),
                    axis.title.x=element_blank(),
                    axis.title.y=element_blank()))
    

    然后您可以“破解”并重新调整因素(也许不是最好的方法,但可以做到)。

    df %>% arrange(Amount) %>% 
        mutate(myColour = factor(myColour, myColour), 
                   Type = factor(Type, Type)) -> df1
    

    这样就更容易将颜色级别作为矢量提取出来。

    mycols <- as.vector(levels(df1$myColour))
    

    然后绘制

    ggplot(df1, aes(Type, Amount, color = myColour, fill = myColour)) + 
               geom_bar(stat = 'identity', position = 'dodge', show.legend = FALSE, width = .85) + 
               theme(axis.text.y = element_text(colour = mycols, size = 18, face = 'bold')) + 
               coord_flip() +
               scale_fill_manual(values = mycols) +
               scale_color_manual(values = mycols)
    

    希望对你有用。

    这是无效的原始编辑,因此可以忽略:在代码中的两个实例中将 df$myColour 更改为 myColour

    有了这么多的主题调整,你真的应该考虑使用theme_set

    【讨论】:

    • 在进行建议的更改后,我收到以下信息:图层错误(数据 = 数据,映射 = 映射,stat = stat,geom = GeomBar,:找不到对象“myColour”
    • 就是这样......我什至不知道tidyverse......谢谢!
    猜你喜欢
    • 1970-01-01
    • 2015-07-14
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2021-03-09
    相关资源
    最近更新 更多