Reputation: 4482
I have the following data.table
decomp1 <- data.table(V2 = c("Sp", "Sp", "Su", "Sp", "Su", "Su", "X",
"Su", "X", "Su", "X", "X", "Sp", "Sp", "X", "Su", "X", "Sp",
"X", "Su", "Sp", "Su", "Su", "X", "Sp", "Sp", "Sp", "Su", "Su",
"X", "Sp", "Sp", "X", "Su", "X", "Su", "Sp", "X", "Su", "Sp",
"Sp", "Su"),
V3 = c("Sp", "Sp", "Su_6", "Sp", "Su_6", "Su_6",
"X", "Su_6", "X", "Su_6", "X", "X", "Sp", "Sp", "X", "Su_6",
"X", "Sp", "X", "Su_6", "Sp", "Su_6", "Su_6", "X", "Sp", "Sp",
"Sp", "Su_6", "Su_6", "X", "Sp", "Sp", "X", "Su_6", "X", "Su_6",
"Sp", "X", "Su_6", "Sp", "Sp", "Su_6"),
V4 = c("Va", "Va", "Pr",
"Va", "Pr", "Pr", "Tr", "He", "He", "He", "Tr", "He", "He", "He",
"Tr", "He", "He", "He", "Qu", "Qu", "Qu", "Qu", "NP", "Qu", "Qu",
"NP", "Qu", "NP", "Qu", "Qu", "NP", "NP", "NP", "NP", "NP", "Tr",
"Tr", "NP", "Tr", "Tr", "Tr", "Tr"),
V5 = c("Non", "Long", "Non",
"Ca", "Long", "Ca", "Non", "Non", "Non", "Long", "Long", "Long",
"Non", "Long", "Ca", "Ca", "Ca", "Ca", "Non", "Non", "Non", "Long",
"Non", "Long", "Long", "Non", "Ca", "Long", "Ca", "Ca", "Long",
"Ca", "Non", "Ca", "Long", "Non", "Non", "Ca", "Long", "Long",
"Ca", "Ca"),
V6 = c("Total", "Total", "Total", "Total", "Total",
"Total", "Total", "Total", "Total", "Total", "Total", "Total",
"Total", "Total", "Total", "Total", "Total", "Total", "Total",
"Total", "Total", "Total", "Total", "Total", "Total", "Total",
"Total", "Total", "Total", "Total", "Total", "Total", "Total",
"Total", "Total", "Total", "Total", "Total", "Total", "Total",
"Total", "Total"),
V1 = c("0.072384480,", "0.005913364,", "0.084688599,",
"0.014997295,", "0.010088229,", "0.020780549,", "0.244325980,",
"0.269335944,", "0.282962793,", "0.018655723,", "0.051010924,",
"0.014688654,", "0.298544133,", "0.026934186,", "0.052777085,",
"0.060859581,", "0.051597466,", "0.025747971,", "0.374481663,",
"0.379704887,", "0.390599270,", "0.030278558,", "0.410034427,",
"0.051577777,", "0.035755516,", "0.440449409,", "0.016192095,",
"0.040907457,", "0.050172704,", "0.037228066,", "0.033683551,",
"0.016825497,", "0.493787903,", "0.045299959,", "0.032781898,",
"0.556733851,", "0.577734392,", "0.063572428,", "0.038519396,",
"0.039316251,", "0.017420358,", "0.044930589)"),
V7 = c(0.07238448,
0.07829784, 0.0846886, 0.09329514, 0.09477683, 0.11555738, 0.24432598,
0.26933594, 0.28296279, 0.28799167, 0.2953369, 0.29765145, 0.29854413,
0.32547832, 0.34811399, 0.34885125, 0.34924891, 0.35122629, 0.37448166,
0.37970489, 0.39059927, 0.40998344, 0.41003443, 0.42605944, 0.42635479,
0.44044941, 0.44254688, 0.45094188, 0.46015615, 0.46328751, 0.47413296,
0.49095846, 0.4937879, 0.49624184, 0.5265698, 0.55673385, 0.57773439,
0.59014223, 0.59525325, 0.61705064, 0.634471, 0.64018384))
and i run
print(ggplot(decomp1,aes(x=V4,y=V1,fill=V5,label=paste0(round(V1,3)*100,"%")))+theme_pl()+geom_bar(stat = "identity",aes(width=0.5))+facet_grid(V3~.)+
scale_fill_manual(values = c(c("Non"="#1581A4", "Long"="#FFA600", "Ca"="#B50000")))+
scale_y_continuous(label=percent,limits = c(0,0.7))+
theme(legend.title=element_blank(),axis.title.x = element_blank(),axis.title.y = element_blank())+
geom_text(aes(y=V7-V1/2),col="white",size=3)+
geom_text(data=decomp1[,.(V1=sum(V1),V5="Non"),by=.(V4,V3)],aes(y=V1,vjust=-1,fontface="bold")))
As you can see the "colors" are not in the correct order. So actually what I would like to have in the end, is the same plot, with the same numbers but the order of the colors (from bottom to top), in each column in each row, should be blue > yellow > red, and not red > yellow > blue. I dont know why is this happening
Upvotes: 0
Views: 72
Reputation: 78792
What version of ggplot2 are you running under? The following:
ggplot(decomp1, aes(x=V4,y=V1,fill=V5, label=paste0(round(V1,3)*100,"%"))) +
geom_bar(stat="identity", width=0.5) +
geom_text(aes(y=V7-V1/2), col="white", size=3) +
geom_text(data=decomp1[,.(V1=sum(V1),V5="Non"), by=.(V4,V3)],
aes(y=V1, vjust=-1), fontface="bold") +
scale_fill_manual(values=c("Non"="#1581A4", "Long"="#FFA600", "Ca"="#B50000")) +
scale_y_continuous(label=scales::percent,limits = c(0,0.7)) +
facet_grid(V3~.) +
theme(legend.title=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())
Generates:
Under the most recent ggplot2 pkg.
We don't have theme_pl()
, hence the difference in theme aesthetics.
I did move some things outside of aes()
that didn't belong there and organized the plot code better (I can't imagine debugging anything in the original format) but factor order determine bar segment order and it seems to be working fine with up-to-date pkgs.
> session_info()
Session info -------------------------------------------------------------------------
setting value
version R version 3.3.2 (2016-10-31)
system x86_64, darwin13.4.0
ui RStudio (1.0.136)
language (EN)
collate en_US.UTF-8
tz America/New_York
date 2016-12-16
Packages -----------------------------------------------------------------------------
package * version date source
assertthat 0.1 2013-12-06 CRAN (R 3.3.0)
colorspace 1.2-6 2015-03-11 CRAN (R 3.3.0)
data.table * 1.9.8 2016-11-25 cran (@1.9.8)
devtools * 1.12.0 2016-06-24 CRAN (R 3.3.0)
digest 0.6.10 2016-08-02 CRAN (R 3.3.0)
ggplot2 * 2.2.0.9000 2016-12-16 Github (hadley/ggplot2@f6f9f9d)
gtable 0.2.0 2016-02-26 CRAN (R 3.3.0)
labeling 0.3 2014-08-23 CRAN (R 3.3.0)
lazyeval 0.2.0 2016-06-12 CRAN (R 3.3.0)
magrittr 1.5 2014-11-22 CRAN (R 3.3.0)
memoise 1.0.0 2016-01-29 CRAN (R 3.3.0)
munsell 0.4.3 2016-02-13 CRAN (R 3.3.0)
plyr 1.8.4 2016-06-08 CRAN (R 3.3.0)
Rcpp 0.12.8 2016-11-17 CRAN (R 3.3.2)
reshape2 1.4.2 2016-10-22 cran (@1.4.2)
rstudioapi 0.6 2016-06-27 CRAN (R 3.3.0)
scales 0.4.1.9000 2016-12-16 Github (hadley/scales@89c2a2f)
stringi 1.1.2 2016-10-01 CRAN (R 3.3.0)
stringr 1.1.0 2016-08-19 CRAN (R 3.3.0)
tibble 1.2 2016-08-26 CRAN (R 3.3.0)
withr 1.0.2 2016-06-20 CRAN (R 3.3.0)
Upvotes: 1