将axis.text标签与ggplot数据框变量中包含的颜色匹配 [英] Matching axis.text labels to colors contained in data frame variable in ggplot

查看:140
本文介绍了将axis.text标签与ggplot数据框变量中包含的颜色匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to create a stacked bar chart where my axis.text takes it's color values from a variable in the data frame that also provides the bar's fill color. This is very important because the consumers of the final visuals will be viewing a series of these bar charts so I need to make sure the colors are consistent for each product type even though the Amount values (and thus the order) will vary. The below is the closest I can get.

# 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

会产生以下内容:

Which produces:

推荐答案

您的因子水平未与因子顺序的更改相对应。

Your factor levels are not mapping with the changes to your factor order.

请注意,我对您的 df 进行了更改,以便在重新订购时确实确实发生了更改,更改位于 Amount 列。

Note that I made a change to your df so that it does indeed change when reordered, the change was in the Amount column.

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

Do yourself a favor and load 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()))

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

You can then 'hack' and relevel the factors (maybe not the best method, but gets it done).

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

然后将颜色级别作为绘制矢量更加容易。

It is then easier to pull out the color levels as a vector for plotting.

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 在代码中的两个实例中。

This is the original edit that didn't work so can be ignore: Change the df$myColour to myColour in two instances in your code.

有了这么多的主题调整,您应该真正想到关于也使用 theme_set

With that many theme tweaks you should really think about using theme_set as well.

这篇关于将axis.text标签与ggplot数据框变量中包含的颜色匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆