使用总分类值标签ggplot Geom_Bar

时间:2019-04-24 18:13:43

标签: r ggplot2 stackedbarseries

使用此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)

要生成此图:

enter image description here

除了我希望标签反映整个堆叠条形的总和,而不是构成堆叠条形的所有单个零件的总和之外,这在任何方面都是完美的。

放大:

enter image description here

我尝试过的事情:  -在此图表后面绘制图表,并用白色条形图绘制-无法弄清楚。  -试图让geom_text引用不同的数据帧表示总值(geom_text(aes(label = melted_total $ value),vjust = -3))。

编辑:

此代码使我非常接近我想要的内容,只需要弄清楚如何在图例中隐藏两个“总计”即可。

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)

输出:

enter image description here

2 个答案:

答案 0 :(得分:2)

reshape2已由tidyr的{​​{1}}和spread()操作取代。通过使用gather(),可以大大简化代码。另外,填充您的因素以使其不可见可能是一种不好的做法。设置tidyverse和设置新的inherit.aes = FALSE参数比较容易。

考虑以下代码:

aes()

答案 1 :(得分:0)

知道了-必须调整 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") +

这会删除白色图例条目。