【发布时间】: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
【问题讨论】: