美好的一天,
我需要准备一张显示各种生物量成分比例的图表,但在 R、Ggplot2 中图例中的每个成分后面显示 ,1。我只想查看图例中的组件列表,每个条目后面不带 ,1。请参阅下面的示例数据、我使用的代码以及带有不必要错误的图表。
此外,该图显示,随着胸径的增加,成分(球果、针叶、树皮、树枝)增加,而它们应该减少。只有茎随着胸径的增加而增加。
样本数据:
Component_order <- c("Cone", "Needle", "Bark", "Branch", "Stem")
colours_order <- c("deepskyblue", "darkslategrey", "darkseagreen", "deeppink2", "darksalmon")
DRProportions$Component <- factor(DRProportions$Component, levels = Component_order)
plotly::ggplotly(
ggplot(DRProportions[order(DRProportions$Component),], aes(x = DBH, y = Percentage, fill = Component)) +
geom_area(alpha=0.6 , linewidth=.5, colour="white") +
scale_colour_manual("", values = c(Cone="deepskyblue", Needle="darkslategray", Bark="darkseagreen", Branch="deeppink2", Stem="darksalmon"))+
labs(y="Percentage (%)", x="DBH (cm)")+
theme(axis.text = element_text(size = 14))+
theme(axis.title = element_text(size = 15))+
theme(legend.text = element_text(size = 15)))
有错误的图表:
我重新排列了图例条目的顺序,并为其分配了相关颜色。这就是 ,1 错误出现的地方。不知道如何进一步修改代码以消除 ,1 。此外,所有组件都显示递增顺序,而它应该只是茎。
问题是您在
fill
aes 上进行映射,但添加了手动色标。相反,切换到 scale_fill_manual
以应用您的自定义颜色 和 以获得所需的图例标签。
使用一些虚假的随机示例数据:
Component_order <- c("Cone", "Needle", "Bark", "Branch", "Stem")
colours_order <- c("deepskyblue", "darkslategrey", "darkseagreen", "deeppink2", "darksalmon")
library(plotly)
set.seed(123)
DRProportions <- expand.grid(
Component = Component_order,
DBH = seq(10, 40, 5)
)
DRProportions$Percentage <- runif(nrow(DRProportions))
DRProportions$Percentage <- ave(DRProportions$Percentage, DRProportions$DBH, FUN = \(x) x / sum(x))
names(colours_order) <- Component_order
DRProportions$Component <- factor(DRProportions$Component, levels = Component_order)
gg <- ggplot(
DRProportions,
aes(x = DBH, y = Percentage, fill = Component)
) +
geom_area(alpha = 0.6, linewidth = .5, colour = "white") +
scale_fill_manual(NULL, values = colours_order) +
labs(y = "Percentage (%)", x = "DBH (cm)") +
theme(axis.text = element_text(size = 14)) +
theme(axis.title = element_text(size = 15)) +
theme(legend.text = element_text(size = 15))
plotly::ggplotly()