使用R ggplot的100%堆积条形图

时间:2017-12-04 22:28:16

标签: r ggplot2

我正在尝试使用以下代码创建100%堆积的条形图:

library(reshape)
datm <- melt(cbind(dat, ind = rownames(dat)), id.vars = c('ind'))

library(scales)
ggplot(datm,aes(x = variable, y = value,fill = ind)) + 
    geom_bar(position = "fill",stat = "identity") +
    # or:
    # geom_bar(position = position_fill(), stat = "identity") 
    scale_y_continuous(labels = percent_format())

来自this回答类似的问题。

我希望我的图表如下所示;在X轴(在下面的例子中将取代亚洲,欧洲和美国)的费用,以及作为彩色堆栈(其大小与其百分比成比例)的年份与总数。

我收到以下错误:

Error: Discrete value supplied to continuous scale

enter image description here

我的样本数据集如下:

structure(list(`Expenses:` = c("Salaries and wages", "Awards and grants", 
"Internet hosting", "In-kind service expenses", "Donations processing expenses", 
"Professional service expenses", "Other operating expenses", 
"Travel and conferences", "Depreciation and amortization", "Special event expense, net", 
"PayPal fees", "Other"), `2015` = c(26049224L, 4522689L, 1997521L, 
235570L, 2484765L, 7645105L, 4449764L, 2289489L, 2656103L, 266552L, 
0L, 0L), `2014` = c(19979908L, 5704791L, 2529483L, 370602L, 1505654L, 
7117519L, 3861708L, 1965854L, 2722007L, 143219L, 0L, 0L), `2013` = c(15983542L, 
2791378L, 2549992L, 260909L, 0L, 0L, 10017121L, 1395013L, 2706841L, 
0L, 0L, 0L), `2012` = c(11749500L, 2106752L, 2486903L, 296599L, 
0L, 0L, 9198892L, 1533150L, 1888856L, 0L, 0L, 0L), `2011` = c(7312120L, 
471060L, 1799943L, 349516L, 0L, 0L, 5761273L, 1159200L, 1000400L, 
36282L, 0L, 0L), `2010` = c(3508336L, 208662L, 1056703L, 502558L, 
0L, 0L, 3846420L, 476663L, 524341L, 70407L, 0L, 72703L), `2009` = c(2257621L, 
0L, 822405L, 578279L, 0L, 0L, 1259161L, 223193L, 419947L, 0L, 
0L, 56630L), `2008` = c(1147679L, 0L, 537204L, 333125L, 0L, 0L, 
952019L, 307679L, 233314L, 0L, 0L, 29704L), `2007` = c(415006L, 
0L, 389417L, 316723L, 0L, 0L, 310334L, 264361L, 349390L, 0L, 
0L, 32612L), `2006` = c(107122L, 0L, 189631L, 114589L, 0L, 0L, 
47777L, 76545L, 143394L, 0L, 45617L, 67232L), `2005` = c(16930L, 
0L, 40273L, 22493L, 0L, 0L, 18067L, 27798L, 41018L, 0L, 11091L, 
0L), `2004` = c(0L, 0L, 0L, 8958L, 0L, 0L, 495L, 293L, 10641L, 
0L, 3076L, 0L)), .Names = c("Expenses:", "2015", "2014", "2013", 
"2012", "2011", "2010", "2009", "2008", "2007", "2006", "2005", 
"2004"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-12L), spec = structure(list(cols = structure(list(`Expenses:` = structure(list(), class = c("collector_character", 
"collector")), `2015` = structure(list(), class = c("collector_integer", 
"collector")), `2014` = structure(list(), class = c("collector_integer", 
"collector")), `2013` = structure(list(), class = c("collector_integer", 
"collector")), `2012` = structure(list(), class = c("collector_integer", 
"collector")), `2011` = structure(list(), class = c("collector_integer", 
"collector")), `2010` = structure(list(), class = c("collector_integer", 
"collector")), `2009` = structure(list(), class = c("collector_integer", 
"collector")), `2008` = structure(list(), class = c("collector_integer", 
"collector")), `2007` = structure(list(), class = c("collector_integer", 
"collector")), `2006` = structure(list(), class = c("collector_integer", 
"collector")), `2005` = structure(list(), class = c("collector_integer", 
"collector")), `2004` = structure(list(), class = c("collector_integer", 
"collector"))), .Names = c("Expenses:", "2015", "2014", "2013", 
"2012", "2011", "2010", "2009", "2008", "2007", "2006", "2005", 
"2004")), default = structure(list(), class = c("collector_guess", 
"collector"))), .Names = c("cols", "default"), class = "col_spec"))

0 个答案:

没有答案