I have an issue with coloring a text of a legend (each group name with corresponding colour) in ggplot2. This is my desired output:
I am using this data:
df <- data.frame(group = c("Group 1","Group 2","Group 3")
, value = c(1000000, 300000, 100000)
, value_format = c("1.000.000", "300.000", "100.000")
, weigth = c("71,4%", "21,4%", "7,14%")
, stringsAsFactors = F)
lables <- paste(paste(df$group, df$value_format, sep = ": "), df$weigth, sep = "\n")
And this is the code with my better aproximation:
library(ggplot2)
library(grid)
library(extrafont)
g <- ggplot(data=df, aes(x = 1, y = value, fill = factor(group)))
g <- g + geom_bar(position="stack",stat="identity")
g <- g + theme(
plot.margin = unit(c(0, 0, 0, 0), "in")
, axis.line = element_blank()
, axis.text.x = element_blank()
, axis.text.y = element_blank()
, axis.ticks = element_blank()
, axis.title.x = element_blank()
, axis.title.y = element_blank()
, panel.background = element_blank()
, panel.border = element_blank()
, panel.grid.major = element_blank()
, panel.grid.minor = element_blank()
, plot.background = element_blank()
, legend.position="top"
, legend.title = element_blank()
, legend.text.align = .5
, legend.text = element_text(colour = c("#595959", "#E26B0A", "#00B0F0")
, family = "Arial"
, size = 11, face = "bold")
, legend.key.size = unit(0.5, "cm")
)
g <- g + scale_fill_manual(values=c("#595959", "#E26B0A", "#00B0F0"),
breaks=c("Group 1","Group 2","Group 3"),
labels=lables)
g <- g + coord_flip()
g
From this point I am not able to make 3 things:
- Color the text corresponding to each group (note that I introduce a vector of colors inside the
element_text
forlegend.text
but it takes the first value only - Remove the legend.key (boxes with colors)
- Distribute a legend text over the bar
Appreciate any help