4

I would like to place axis text above the appropriate horizontal bars in a ggplot2 plot. Below is as far as I have gotten, with the plotting code afterwards. The data is at the bottom of this question.

My questions, aside from the ever-present "what code would accomplish the goal better", are (1) instead of my manually entering rectangle and text locations, how can R place them algorithmically, (2) how can R move the text in the rectangles to the far left (I tried with calculating a mid-point based on the number of characters in the text, but it doesn't work)?

enter image description here

For the plotting I created the sequence variable instead of struggling with as.numeric(as.character(risks).

ggplot(plotpg19, aes(x = sequence, y = scores)) +
  geom_bar(stat = "identity", width = 0.4) +
  coord_flip() +
  labs(x = "", y = "") +
  theme_bw() +
  theme(axis.text.y = element_blank(), axis.ticks.y = element_blank()) +
  geom_rect(data=plotpg19, aes(xmin= seq(1.5, 8.5, 1), 
                               xmax= seq(1.8, 8.8, 1), ymin=0, ymax=30), fill = "white") +
  geom_text(data=plotpg19, aes(x=seq(1.6, 8.6, 1), y= nchar(as.character(risks))/2, label=risks, size = 5, show_guide = FALSE)) +
  guides(size = FALSE)

Below is the data.

plotpg19 <- structure(list(risks = structure(c(8L, 7L, 6L, 5L, 4L, 3L, 2L, 
1L), .Label = c("Other", "Third parties/associates acting on our behalf", 
"Rogue employees", "Lack of understanding by top executives", 
"Lack of anti-bribery/corruption training or awareness within the business", 
"Geographic locations in which the company operates", "Industries/sector(s) in which the company operates", 
"Inadequate resources for anti-bribery/corruption compliance activities"
), class = "factor"), scores = c(15, 28, 71, 16, 5, 48, 55, 2
), sequence = 1:8), .Names = c("risks", "scores", "sequence"), class = "data.frame", row.names = c(NA, 
-8L))

This question gave me some guidance. fitting geom_text inside geom_rect

Community
  • 1
  • 1
lawyeR
  • 7,488
  • 5
  • 33
  • 63

1 Answers1

4

I do not understand why your are plotting white geom_rect. For the second question, setting y=0 in the aes of geom_text and adding hjust=0 (start the text at precisely y) works. I adjusted the x parameter so that the text are plotted halfway through bars :

library(dplyr)
plotpg19 <- mutate(plotpg19, xtext = sequence + 0.55)

library(ggplot2)
ggplot(plotpg19, aes(x = sequence, y = scores)) +
  geom_bar(stat = "identity", width = 0.4) +
  coord_flip() +
  labs(x = "", y = "") +
  theme_bw() +
  theme(axis.text.y = element_blank(), axis.ticks.y = element_blank()) +
  geom_text(data = plotpg19,
            aes(x = xtext, y = 0, label = risks, size = 5, show_guide = FALSE),
            hjust = 0, vjust = 1) +
  guides(size = FALSE)

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
scoa
  • 19,359
  • 5
  • 65
  • 80
  • Very good and geom_text on its own seems fine. How can R figure out the aes(x=seq(1.55, 8.55, 1) starting point and end points automatically? – lawyeR Jul 20 '15 at 14:33
  • @lawyeR IMHO, it is better to set it yourself. You can do it directly in your original data.frame rather than in the ggplot command (see my edit). You could also map x to sequence and then play with vjust, but I would not recommend it (: `geom_text(data=plotpg19, aes(x=sequence, y= 0, label=risks, size = 5, show_guide = FALSE),hjust=0,vjust=-3.5) ` almost does the trick, but you"ll have to adjust the plot height manually. – scoa Jul 20 '15 at 14:42