I have made a plot, but i have two issues that i need help with.
Question 1)
How do i centre the labels in each section of the bars? While searching for a solution, i have tried to change the line:
geom_text(aes(label= paste(round(pct),"%")), color = "white", size=2.6, position="fill", stat="identity")
to
geom_text(aes(label= paste(round(pct),"%")), color = "white", size=2.6, position=position_stack(vjust = 0.5))
This puts the labels in the correct place, but now the colored bars are not printet.
Question 2)
Where the plot will be used, it is going to be rather narrow like in this picture: .
Is there a way to automatically deal with overlapping labels? Another helpfull solution could be to offset every other label, so that first label is above the middle of the bar, and the second label is below the middle (I hope this makes sense). Other solutions are also welcome.
Code to reproduce my plot:
library(ggplot2)
exdata <- structure(list(Var1 = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L,
4L), .Label = c("Meget ofte", "Ofte", "Nogle gange", "Aldrig eller næsten aldrig"
), class = "factor"), Freq = c(4L, 16L, 50L, 38L, 6L, 5L, 55L,
43L), spm = c("Question 1: aaaaaabbbbbbbbbcccccc", "Question 1: aaaaaabbbbbbbbbcccccc",
"Question 1: aaaaaabbbbbbbbbcccccc", "Question 1: aaaaaabbbbbbbbbcccccc",
"Question 2: aaaaaabbbbbbbbbcccccc", "Question 2: aaaaaabbbbbbbbbcccccc",
"Question 2: aaaaaabbbbbbbbbcccccc", "Question 2: aaaaaabbbbbbbbbcccccc"
), pct = c(3.7037037037037, 14.8148148148148, 46.2962962962963,
35.1851851851852, 5.5045871559633, 4.58715596330275, 50.4587155963303,
39.4495412844037)), row.names = 21:28, class = "data.frame")
palette1 <- c("#00ABA4", "#008983", "#006763", "#004543")
plot1 <- ggplot(data=exdata, aes(y=Freq, x=spm, fill= fct_rev(Var1))) +
geom_bar(position="fill", stat="identity") +
scale_fill_manual(values=c(palette1)) +
geom_text(aes(label= paste(round(pct),"%")), color = "white", size=2.6, position="fill", stat="identity") +
coord_flip() +
labs(fill = "Svar") +
labs(title = "Hvor often do you do these things?") +
ylab("Percentage") +
xlab("Question") +
theme(legend.position="bottom", legend.title=element_text(size=10)) +
guides(fill =guide_legend(reverse = TRUE,nrow=2,byrow=TRUE))
plot1
You could maybe try to use geom_text_repel()
from the ggrepel
package. I would also recommend you switch back position="fill"
to place the labels properly. The point to note is that you should also apply vjust=
to the position=
adjustment for the text just like you tried with position_stack()
.
Without repelling labels, here's what it looks like (note that I changed your data so that more overlap between labels would be evident):
# all code the same except for this line
geom_text(aes(label= paste0(round(pct),"%")),
color = "white", size=2.6, position=position_fill(vjust = 0.5))
With geom_text_repel
, it looks like this. Be sure to load library(ggrepel)
:
geom_text_repel(aes(label= paste0(round(pct),"%")),
direction='y',color = "white", size=2.6,
position=position_fill(vjust = 0.5))