Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you create a box around an axis tick label in ggplot2?

For several reasons, I am trying to duplicate the grotesque plot shown below. It violates many precepts of good data visualization so for training purposes my goal is to use ggplot2 and deconstruct it -- remove or revise poorly-chosen features one at a time. Using the data reproduced at the bottom and the code below the plot, I am getting close but have been unable to figure out how to include one notable feature.

Question: Is there a way to reproduce the black shaded rectangle around the three tick labels? (If so, it is straightforward to create another factor variable to identify those three labels and change their font to white.)

enter image description here

ggplot(plotpg19, aes(x = risks, y = scores, fill = colors)) +
  geom_bar(stat = "identity", width = 0.6) +
  scale_fill_manual(values = c("grey50", "deepskyblue2", "mediumorchid3", "gold")) +
  geom_text(aes(label = scores), hjust = -0.4, size = 8, face = "bold") +
  coord_flip() +
  theme_bw() + labs(x = NULL, y = NULL) +
  theme(panel.grid.major = element_blank()) +
  guides(fill = FALSE) +
  scale_y_continuous(breaks = seq(0, 100, 20), labels = seq(0, 100, 20), expand = c(0, 0)) +
  theme(
    panel.border = element_blank(),
    axis.line = element_line(colour = "black", size = 5, linetype = "solid", lineend = "square")
  ) +
  geom_hline(yintercept = seq(0, 80, 10), colour = "grey30", size = .5, linetype = "dotted") +
  theme(axis.line.x = element_blank()) # to get a single axis border, must format both and then blank one

enter image description here

BTW, this question uses symbols for tick labels may offer some insights using the grImport package but its teachings are beyond me and I am looking for a shaded box around designated tick labels.

plotpg19 <- structure(list(risks.format = structure(c(2L, 3L, 1L, 4L, 5L, 
  7L, 8L, 6L), .Label = c("Geographic locations in which\n                                 the company operates", 
  "Inadequate resources for anti-bribery/\ncorruption compliance activities", 
  "Industries/sector(s) in which\n                                 the company operates", 
  "Lack of anti-bribery/corruption training\n                                 or awareness within the business", 
  "Lack of understanding\n                                 by top executives", 
  "Other", "Rogue employees", "Third parties/associates\n                                 acting on our behalf"
  ), class = "factor"), 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
  ), colors = c("A", "A", "B", "A", "A", "C", "D", "A")), .Names = c("risks.format", 
  "risks", "scores", "colors"), row.names = c(NA, -8L), class = "data.frame")
like image 487
lawyeR Avatar asked Jul 12 '15 17:07

lawyeR


People also ask

How do I change a tick label in R?

Option 1. Set xaxt = "n" and yaxt = "n" to remove the tick labels of the plot and add the new labels with the axis function. Note that the at argument sets where to show the tick marks.

How do you set Y axis ticks?

Specify y-Axis Tick Values for Specific Axes Call the nexttile function to create the axes objects ax1 and ax2 . Plot data into each axes. Set the y-axis ticks for the lower plot by passing ax2 as the first input argument to the yticks function.

What are axis tick labels?

The tick labels are the labels that you see next to each tick mark. The tick values are the locations along the x-axis where the tick marks appear. Set the values using the xticks function. Set the corresponding labels using the xticklabels function.

How do you stop axis labels overlapping in Ggplot?

To avoid overlapping labels in ggplot2, we use guide_axis() within scale_x_discrete().


1 Answers

I think your best bet is to create viewports at the location of the tick labels, and draw a black rectangle and white text there. For example:

vp1 <- viewport(x = 0.225, y = 0.55, width = 0.45, height = 0.07)
grid.rect(gp = gpar(fill="black"), vp = vp1)
grid.text("Lack of anti-bribery corruption training or awareness within the business", gp=gpar(col="white", cex = 0.6), vp = vp1)

Will give you the graph below:enter image description here

like image 128
RHA Avatar answered Sep 29 '22 03:09

RHA