用总分类值标记 ggplot Geom_Bar
Label ggplot Geom_Bar with total categorical value
使用此 CSV:
Year,Permanent Wetland Loss,Permit Wetlands CRE,Permit Conservation,ARM Conservation,ARM Restoration,ARM Enhancement,Conservation_Total,EnRes_Total
2008,61,4,1271,,,,1271,4
2009,73,4,2707,1403,,,4110,4
2010,70,26,1440,1030,,,2470,26
2011,52,32,781,2537,,,3318,32
2012,41,8,211,2675,,,2886,8
2013,68,21,265,2191,6.6,80,2456,107.6
2014,48,1,114,1165,,,1279,1
2015,73,0,947,2381,11,,3328,11
2016,33,18,116,3751,,,3867,18
2017,59,15,136,,,,136,15
2018,77,1,89,8177,,,8266,1
我正在执行这段代码:
library(reshape2) # for melt
input_df <- read.csv("ARM_PERMIT_COMB.csv", header=TRUE)
names(input_df) <- c('Year', 'Wetland Loss','Restoration/Enhancement - Permit','Conservation - Permit',
'Conservation - ARM', 'Restoration - ARM', 'Enhancement - ARM', 'Con - Total', 'EnRes - Total')
input_df <- input_df[,c(1,5,4,3,6,7,2)]
melted <- melt(input_df, "Year")
melted$cat <- ''
melted[melted$variable == 'Wetland Loss',]$cat <- "Loss"
melted[melted$variable == 'Restoration/Enhancement - Permit',]$cat <- "Enhancement / Restoration"
melted[melted$variable == 'Restoration - ARM',]$cat <- "Enhancement / Restoration"
melted[melted$variable == 'Enhancement - ARM',]$cat <- "Enhancement / Restoration"
melted[melted$variable == 'Conservation - ARM',]$cat <- "Conservation"
melted[melted$variable == 'Conservation - Permit',]$cat <- "Conservation"
ggplot(melted, aes(x = cat, y = value, fill = variable)) +
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ Year) +
labs(title = 'Wetlands Loss, Conservation, Enhancement, Restoration, ', y='Acres', x='', subtitle = 'Years 2008 - 2018') +
theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.3, hjust=1)) +
scale_fill_manual(values=c("chartreuse2","green4", "steelblue3", "yellow3","orangered2", "grey33","white","white"),
name="Impacts and\nMitigation") +
geom_text(aes(label=value), vjust = -3)
要生成此图表:
除了我希望标签反映整个堆叠条形图的总和而不是构成堆叠条形图的所有单个部分之外,这在各个方面都是完美的。
放大:
我尝试过的事情:
- 在这个图表后面用白色条绘制图表 - 无法弄清楚。
- 试图让 geom_text 引用一个不同的数据帧,它表示总计 (geom_text(aes(label=melted_total$value), vjust = -3)) 没有用.
编辑:
这段代码让我非常非常接近我想要的,只需要弄清楚如何隐藏图例中的两个 'Totals':
library(reshape2) # for melt
input_df <- read.csv("ARM_PERMIT_COMB.csv", header=TRUE)
input_total_df <- input_df[,c(1,2,8,9)]
names(input_df) <- c('Year', 'Wetland Loss','Restoration/Enhancement - Permit','Conservation - Permit',
'Conservation - ARM', 'Restoration - ARM', 'Enhancement - ARM', 'Con - Total', 'EnRes - Total')
names(input_total_df) <- c('Year', 'Wetland Loss','Con - Total', 'EnRes - Total')
input_df <- input_df[,c(1,5,4,3,6,7,2)]
melted <- melt(input_df, "Year")
melted_total <- melt(input_total_df, "Year")
melted$cat <- ''
melted[melted$variable == 'Wetland Loss',]$cat <- "Loss"
melted[melted$variable == 'Restoration/Enhancement - Permit',]$cat <- "Enhancement / Restoration"
melted[melted$variable == 'Restoration - ARM',]$cat <- "Enhancement / Restoration"
melted[melted$variable == 'Enhancement - ARM',]$cat <- "Enhancement / Restoration"
melted[melted$variable == 'Conservation - ARM',]$cat <- "Conservation"
melted[melted$variable == 'Conservation - Permit',]$cat <- "Conservation"
melted_total$cat <- ''
melted_total[melted_total$variable == 'Wetland Loss',]$cat <- "Loss"
melted_total[melted_total$variable == 'Con - Total',]$cat <- "Conservation"
melted_total[melted_total$variable == 'EnRes - Total',]$cat <- "Enhancement / Restoration"
ggplot(melted, aes(x = cat, y = value, fill = variable)) +
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ Year) +
labs(title = 'Wetlands Loss, Conservation, Enhancement, Restoration, ', y='Acres', x='', subtitle = 'Years 2008 - 2018') +
theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.3, hjust=1)) +
scale_fill_manual(values=c("white","chartreuse2","green4", "steelblue3", "white", "yellow3","orangered2", "grey33", "white"),
name="Impacts and\nMitigation") +
geom_text(data=melted_total, aes(label=value), vjust = -1, size=2)
输出:
知道了 - 必须调整 scale_fill_manual:
中的中断
scale_fill_manual(breaks=c('Year', 'Wetland Loss','Restoration/Enhancement - Permit','Conservation - Permit',
'Conservation - ARM', 'Restoration - ARM', 'Enhancement - ARM'),
values=c("white","chartreuse2","green4", "steelblue3", "white", "yellow3","orangered2", "grey33", "white"),
name="Impacts and\nMitigation") +
然后删除白色图例条目。
reshape2
已被 tidyr
的 spread()
和 gather()
操作所取代。使用 tidyverse
可以极大地简化代码。此外,填充您的因素以使其不可见可能是一种不好的做法。设置 inherit.aes = FALSE
和设置新的 aes()
参数更容易。
考虑这段代码:
library(tidyverse)
input_df <- read_csv("ARM_PERMIT_COMB.csv") %>%
magrittr::set_colnames(
c('Year', 'Wetland Loss','Restoration/Enhancement - Permit',
'Conservation - Permit', 'Conservation - ARM', 'Restoration - ARM',
'Enhancement - ARM', 'Con - Total', 'EnRes - Total')
) %>%
select(-contains("Total")) %>%
gather(Variable, Value, -Year) %>%
mutate(Category = case_when(
Variable == "Wetland Loss" ~ "Loss",
str_detect(Variable, "Restoration|Enhancement") ~ "Enhancement / Restoration",
str_detect(Variable, "Con") ~ "Conservation",
TRUE ~ "Enhancement / Restoration"
))
ggplot(input_df, aes(x = Category, y = Value, fill = Variable)) +
geom_bar(stat = "identity", position = "stack") +
facet_grid(~Year) +
labs(title = 'Wetlands Loss, Conservation, Enhancement, Restoration, ',
y = 'Acres', x = '', subtitle = 'Years 2008 - 2018') +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 90, vjust = 0.3, hjust = 1)) +
scale_fill_manual(values = c("chartreuse2","green4", "steelblue3",
"yellow3","orangered2", "grey33"),
name = "Impacts and\nMitigation") +
geom_text(data = input_df %>%
group_by(Year, Category) %>%
summarise(Value = sum(Value, na.rm = TRUE)),
aes(label = Value, x = Category, y = Value), inherit.aes = FALSE,
vjust = -1, size = 2)
使用此 CSV:
Year,Permanent Wetland Loss,Permit Wetlands CRE,Permit Conservation,ARM Conservation,ARM Restoration,ARM Enhancement,Conservation_Total,EnRes_Total
2008,61,4,1271,,,,1271,4
2009,73,4,2707,1403,,,4110,4
2010,70,26,1440,1030,,,2470,26
2011,52,32,781,2537,,,3318,32
2012,41,8,211,2675,,,2886,8
2013,68,21,265,2191,6.6,80,2456,107.6
2014,48,1,114,1165,,,1279,1
2015,73,0,947,2381,11,,3328,11
2016,33,18,116,3751,,,3867,18
2017,59,15,136,,,,136,15
2018,77,1,89,8177,,,8266,1
我正在执行这段代码:
library(reshape2) # for melt
input_df <- read.csv("ARM_PERMIT_COMB.csv", header=TRUE)
names(input_df) <- c('Year', 'Wetland Loss','Restoration/Enhancement - Permit','Conservation - Permit',
'Conservation - ARM', 'Restoration - ARM', 'Enhancement - ARM', 'Con - Total', 'EnRes - Total')
input_df <- input_df[,c(1,5,4,3,6,7,2)]
melted <- melt(input_df, "Year")
melted$cat <- ''
melted[melted$variable == 'Wetland Loss',]$cat <- "Loss"
melted[melted$variable == 'Restoration/Enhancement - Permit',]$cat <- "Enhancement / Restoration"
melted[melted$variable == 'Restoration - ARM',]$cat <- "Enhancement / Restoration"
melted[melted$variable == 'Enhancement - ARM',]$cat <- "Enhancement / Restoration"
melted[melted$variable == 'Conservation - ARM',]$cat <- "Conservation"
melted[melted$variable == 'Conservation - Permit',]$cat <- "Conservation"
ggplot(melted, aes(x = cat, y = value, fill = variable)) +
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ Year) +
labs(title = 'Wetlands Loss, Conservation, Enhancement, Restoration, ', y='Acres', x='', subtitle = 'Years 2008 - 2018') +
theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.3, hjust=1)) +
scale_fill_manual(values=c("chartreuse2","green4", "steelblue3", "yellow3","orangered2", "grey33","white","white"),
name="Impacts and\nMitigation") +
geom_text(aes(label=value), vjust = -3)
要生成此图表:
除了我希望标签反映整个堆叠条形图的总和而不是构成堆叠条形图的所有单个部分之外,这在各个方面都是完美的。
放大:
我尝试过的事情: - 在这个图表后面用白色条绘制图表 - 无法弄清楚。 - 试图让 geom_text 引用一个不同的数据帧,它表示总计 (geom_text(aes(label=melted_total$value), vjust = -3)) 没有用.
编辑:
这段代码让我非常非常接近我想要的,只需要弄清楚如何隐藏图例中的两个 'Totals':
library(reshape2) # for melt
input_df <- read.csv("ARM_PERMIT_COMB.csv", header=TRUE)
input_total_df <- input_df[,c(1,2,8,9)]
names(input_df) <- c('Year', 'Wetland Loss','Restoration/Enhancement - Permit','Conservation - Permit',
'Conservation - ARM', 'Restoration - ARM', 'Enhancement - ARM', 'Con - Total', 'EnRes - Total')
names(input_total_df) <- c('Year', 'Wetland Loss','Con - Total', 'EnRes - Total')
input_df <- input_df[,c(1,5,4,3,6,7,2)]
melted <- melt(input_df, "Year")
melted_total <- melt(input_total_df, "Year")
melted$cat <- ''
melted[melted$variable == 'Wetland Loss',]$cat <- "Loss"
melted[melted$variable == 'Restoration/Enhancement - Permit',]$cat <- "Enhancement / Restoration"
melted[melted$variable == 'Restoration - ARM',]$cat <- "Enhancement / Restoration"
melted[melted$variable == 'Enhancement - ARM',]$cat <- "Enhancement / Restoration"
melted[melted$variable == 'Conservation - ARM',]$cat <- "Conservation"
melted[melted$variable == 'Conservation - Permit',]$cat <- "Conservation"
melted_total$cat <- ''
melted_total[melted_total$variable == 'Wetland Loss',]$cat <- "Loss"
melted_total[melted_total$variable == 'Con - Total',]$cat <- "Conservation"
melted_total[melted_total$variable == 'EnRes - Total',]$cat <- "Enhancement / Restoration"
ggplot(melted, aes(x = cat, y = value, fill = variable)) +
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ Year) +
labs(title = 'Wetlands Loss, Conservation, Enhancement, Restoration, ', y='Acres', x='', subtitle = 'Years 2008 - 2018') +
theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.3, hjust=1)) +
scale_fill_manual(values=c("white","chartreuse2","green4", "steelblue3", "white", "yellow3","orangered2", "grey33", "white"),
name="Impacts and\nMitigation") +
geom_text(data=melted_total, aes(label=value), vjust = -1, size=2)
输出:
知道了 - 必须调整 scale_fill_manual:
中的中断 scale_fill_manual(breaks=c('Year', 'Wetland Loss','Restoration/Enhancement - Permit','Conservation - Permit',
'Conservation - ARM', 'Restoration - ARM', 'Enhancement - ARM'),
values=c("white","chartreuse2","green4", "steelblue3", "white", "yellow3","orangered2", "grey33", "white"),
name="Impacts and\nMitigation") +
然后删除白色图例条目。
reshape2
已被 tidyr
的 spread()
和 gather()
操作所取代。使用 tidyverse
可以极大地简化代码。此外,填充您的因素以使其不可见可能是一种不好的做法。设置 inherit.aes = FALSE
和设置新的 aes()
参数更容易。
考虑这段代码:
library(tidyverse)
input_df <- read_csv("ARM_PERMIT_COMB.csv") %>%
magrittr::set_colnames(
c('Year', 'Wetland Loss','Restoration/Enhancement - Permit',
'Conservation - Permit', 'Conservation - ARM', 'Restoration - ARM',
'Enhancement - ARM', 'Con - Total', 'EnRes - Total')
) %>%
select(-contains("Total")) %>%
gather(Variable, Value, -Year) %>%
mutate(Category = case_when(
Variable == "Wetland Loss" ~ "Loss",
str_detect(Variable, "Restoration|Enhancement") ~ "Enhancement / Restoration",
str_detect(Variable, "Con") ~ "Conservation",
TRUE ~ "Enhancement / Restoration"
))
ggplot(input_df, aes(x = Category, y = Value, fill = Variable)) +
geom_bar(stat = "identity", position = "stack") +
facet_grid(~Year) +
labs(title = 'Wetlands Loss, Conservation, Enhancement, Restoration, ',
y = 'Acres', x = '', subtitle = 'Years 2008 - 2018') +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 90, vjust = 0.3, hjust = 1)) +
scale_fill_manual(values = c("chartreuse2","green4", "steelblue3",
"yellow3","orangered2", "grey33"),
name = "Impacts and\nMitigation") +
geom_text(data = input_df %>%
group_by(Year, Category) %>%
summarise(Value = sum(Value, na.rm = TRUE)),
aes(label = Value, x = Category, y = Value), inherit.aes = FALSE,
vjust = -1, size = 2)