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:
element_text
for legend.text
but it takes the first value only Appreciate any help
I've resolved the problem using annotate
function, as @Axeman suggested below. This is my final code (with some adjustment's tricks):
library(ggplot2)
library(grid)
library(extrafont)
g <- ggplot(data = df, aes(x = 1, y = value, fill = factor(group))) +
geom_bar(position = "stack", stat = "identity") +
theme(plot.margin = unit(c(.5, 0.2, 0, 0.1), "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=""
) +
scale_fill_manual(values=c("#595959", "#E26B0A", "#00B0F0"),
breaks=c("Group 1","Group 2","Group 3"),
labels=lables) +
coord_flip() +
annotate("text", label=lables[1], x=1.8, y = df$value[1]/2
, fontface = "bold", color = "#595959", family="Arial", size = 4.5) +
annotate("text", label=lables[2], x=1.8, y = sum(df$value)-400000
, fontface = "bold", color = "#E26B0A", family="Arial", size = 4.5) +
annotate("text", label=lables[3], x=1.8, y = sum(df$value)-100000
, fontface = "bold", color = "#00B0F0", family="Arial", size = 4.5)
g
Thank's!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.