将axis.text标签与ggplot数据框变量中包含的颜色匹配

麦克·勒杜布

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

# 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

产生: 在此处输入图片说明

威廉姆斯

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

请注意,我对您进行了更改,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$myColourmyColour代码中的两个实例。

进行了如此多的主题调整后,您还应该真正考虑使用theme_set

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将ggplot图例颜色与数据框中的实际颜色匹配

ggplot将颜色代码列与R数据帧中的另一列匹配

将颜色渐变与Gnuplot中的数据匹配

熊猫数据框groupby将标签包含在numpy数组中

如何将下标添加到ggplot2 axis.text

将变量值与列匹配以在数据框中检索值

根据将 1 个数据框中的 2 个变量与“答案键”数据框中的“键”匹配,从“答案键”数据框中获取值

R:如何将变量转换为 ggplot 数据框中的值

Matplotlib:手动将更多颜色添加到axis.prop_cycle()中的默认颜色?

如何将d3.svg.axis限制为整数标签?

ggplot sec_axis我可以将向量用于反式公式吗?

在 ggplot 的axis.text.x 名称中添加多种颜色

将像素颜色存储在数据框中

我可以在Axis 2 Java中调用存根之前将属性设置为MessageContext吗?

使用JPanel的Y_AXIS约束将JLabel对齐到BoxLayout中的左侧或右侧

如何在不丢失变量标签的情况下将所有因子变量转换为大型数据框中的数字变量?

将标签列插入数据框中的列内容

通过将列名与数据框中的变量匹配,将值从一个数据框中分配到另一个数据框中

将字符串与不同数据框中的值匹配

传单将标签文本颜色与对象颜色匹配

将变量与复选框标签匹配

将数据框中的行列组合分配给变量

将pandastable数据框存储到变量中

将列表变量绑定到新的数据框中

如何将数据框的列的值与其他数据框中的正确行匹配?

将包含可变长度列表的数据框列转换为数据框中的多列

将包含数据框名称的列添加到数据框列表中

将值与部分匹配进行比较,然后将匹配的数据存储到新变量中

根据列中的正则表达式语法,将变量添加到包含每一行最大值的数据框中