对于条形图,我希望按自定义顺序垂直堆叠条形组件。在下面的示例中,应该控制堆叠顺序的变量是“机制”。预期的堆叠顺序既不是字母顺序,也不是数据行排序的顺序。并非所有条形图都有所有机制。我手动设置了序列,并将序列作为一个因子的级别使用。完全缺失的因子级别没有得到正确的处理,对此我们欢迎使用解决方案,但我可以解决这个问题。
要添加显示每个组件大小的文本标签,我计算标签的y高度值并添加一个geom_text. --我希望文本的颜色与条形组件的颜色形成一致的对比:蓝色条形组件上的白色文本,等等。因此,我将geom_ text的颜色分配给与geom_ bar的填充相同的变量。但是,背景和文本的预期颜色配对不保持匹配。请给出一些新的想法。(感谢编辑插入了这张混乱的图表的图片。)
期望的结果是,每一个机制为“热”的条形组件都会有带有黑色文本注释的红色填充。每个机制为“城市”的组件都将使用tan4填充grey50文本进行注释。
我还尝试分配颜色,而不将映射的变量设置为一个因子(例如选项1),就像在geom_text() & color gradient中一样。
library(ggplot2)
m.data <- data.frame(m.model = factor(c(1, 1, 2, 2, 4)),
mechanism = c("urban", "hot", "hot", "urban", "urban"),
bar.height = 1:5,
y.for.text = c(.5, 2, 1.5, 5, 2.5))
mechanism.order <- c("hot", "dry", "urban")
mechanism.colors <- c("red", "blue", "tan4")
text.colors <- c("black", "white", "grey50")
m.map <- ggplot() +
geom_bar(data = m.data, aes(x = m.model, y = bar.height,
fill = factor(mechanism, levels = mechanism.order)),
stat = "identity") +
scale_fill_manual(values = mechanism.colors) +
scale_color_manual(values = text.colors)
# option 1:
m.map +
geom_text(data = m.data, aes(x = m.model, y = y.for.text,
color = mechanism, label = bar.height))
# option 2:
m.map +
geom_text(data = m.data, aes(x = m.model, y = y.for.text,
color = factor(mechanism, levels = mechanism.order),
label = bar.height))
发布于 2022-03-20 09:32:06
要明确颜色,请将它们作为一个名为的向量传递。
这里还有几点:
使用geom_col
geom_bar
stat = "identity"
只是很长的一段路,不需要为文本提供代码y位置。如果您的层都使用相同的数据和x,y变量,则只需在position_stack(vjust = 0.5)
geom_text
中使用ggplot
调用传递它们,并让层继承它们。
mechanism.colors <- c(hot = "red", dry = "blue", urban = "tan4")
text.colors <- c(hot = "black", dry = "white", urban = "gray50")
ggplot(data = m.data, aes(x = m.model, y = bar.height)) +
geom_col(aes( fill = factor(mechanism, levels = mechanism.order))) +
geom_text(aes(label = bar.height, color = mechanism),
position = position_stack(vjust = 0.5), size = 8) +
scale_fill_manual(values = mechanism.colors) +
scale_color_manual(values = text.colors) +
labs(fill = "mechanism") +
guides(color = guide_none())
发布于 2022-03-20 09:31:42
我不确定我是否理解您想要的输出,但这可能会有所帮助:
我稍微更新了您的ggplot调用:如果您在最初的ggplot调用中提供了aes()
,则不需要在之后重复。此外,不需要将factor()
添加到fill =
语句。在geom_text
中,将color = mechanism
放入aes()中,以便以后可以使用scale_color_manual
访问它以添加自定义颜色。最后,您可以添加position = position_stack
以获得中间的文本。
ggplot(m.data, aes(m.model, bar.height, fill = mechanism)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = mechanism.colors) +
geom_text(aes(label = bar.height, color = mechanism), position = position_stack(.5)) +
scale_color_manual(values = c("black", "grey50 "))
输出:
然而,正如你所看到的,gray40可能不是蓝色背景上最好的颜色,因为对比度不是很好。你可能想选择另一种颜色。
发布于 2022-03-20 09:48:05
建议使用修改后的数据。大部分的程序已经由亲爱的阿兰卡梅隆介绍。新的是转换成因子之前的is图:
m.data <- data.frame(m.model = factor(c(1, 1, 2, 2, 4)),
mechanism = c("urban", "hot", "dry", "urban", "urban"),
bar.height = 1:5,
y.for.text = c(.5, 2, 1.5, 5, 2.5))
mechanism.order <- c("hot", "dry", "urban")
mechanism.colors <- c("red", "blue", "tan4")
text.colors <- c("black", "white", "grey50")
library(tidyverse)
m.data %>%
mutate(mechanism = factor(mechanism, levels = mechanism.order)) %>%
ggplot(aes(x=m.model, y=bar.height, fill=mechanism))+
geom_col()+
geom_text(aes(label = bar.height, color = mechanism),
position = position_stack(vjust = 0.5)) +
scale_fill_manual(values = mechanism.colors) +
scale_color_manual(values = text.colors)
https://stackoverflow.com/questions/71548629
复制相似问题