Refining ggplot R code so annotated rectangle appe

2019-07-26 07:17发布

问题:

Using the following data, I am trying to create a graph for a presentation:

name = c("Phil", "Ian", "Leslie", "Darla", "Silvia", "Ron", "Emily", "Jack")
score = c(73.8, 73.5, 70.3, 68.9, 65.3, 61.4, 55.2, 54.3)
tx = c(0,0,1,0,0,0,0,0)
test1 <- data.frame(name, score, tx)

The graph is meant to show each point as a large circle with the value of the plot point inside the circle. A red dashed line represents the mean of the values plotted. The grey rectangle represents the standard deviation around the mean. Here is the code that I am using to create the graph:

library(ggplot2)
library(scales)

cols <- c("1" = "#ff7f0e", "0" = "#1f77b4")
txctrl <- c("1" = "#ffffff", "0" = "#000000")

ggplot(test1, aes(x=score, y=reorder(name,score))) +
  geom_vline(aes(xintercept=mean(score)), colour="red", linetype="dashed", size=1) +
  geom_point(aes(colour = factor(tx), size=0), show.legend = FALSE) +
  annotate("rect", xmin=(mean(test1$score)-sd(test1$score)), xmax=(mean(test1$score)+sd(test1$score)), ymin=-Inf, ymax=Inf, alpha=0.5, fill="grey90") +
  #  geom_label(aes(label=score), colour=txctrl, fontface = "bold") +
  scale_colour_manual(values=cols) +
  scale_size(range=c(11,11)) +
  xlim(52,78) +
  geom_text(aes(label=score), hjust=0.5, vjust=0.5, size=5, label=round(test1$score, digits = 0)) +
  labs(y='Name', x='Score') +
  theme_bw() +
  theme(panel.border = element_blank(), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        axis.line = element_line(color=NA),
        axis.ticks = element_blank(),
        axis.text.x = element_blank(),
        axis.title.x = element_blank(),
        axis.text.y = element_text(size=14),
        axis.title.y = element_blank())

Here is the result of the code above:

The problem I am running into is the rectangle is fading the color of the plot points. How can I get the grey rectangle to appear behind the plot points and value labels?

Working layer by layer, I managed to get the dashed mean line to sit behind the plot points. When I try to move the annotated rectangle anywhere before the layer identifying the plot points, I get an error.

回答1:

Try using geom_rect instead of annotate. Then, you'll want to remove your aesthetics from the aes function. I can't say why it produces an error, but that it does.

You can study below to see what I mean.

 ggplot(test1, aes(x=score, y=reorder(name,score))) +
 geom_rect(xmin=(mean(score)-sd(score)), xmax=(mean(score)+sd(score)),ymin=-Inf, ymax=Inf, alpha=0.5,fill="grey90") +
 geom_vline(aes(xintercept=mean(score)), colour="red", linetype="dashed", size=1) +
 geom_point(aes(colour = factor(tx), size=0), show.legend = FALSE) +
 scale_colour_manual(values=cols) +
 scale_size(range=c(11,11)) +
 xlim(52,78) +
 geom_text(aes(label=score), hjust=0.5, vjust=0.5, size=5, label=round(test1$score, digits = 0)) +
 labs(y='Name', x='Score') +
 theme_bw() +
 theme(panel.border = element_blank(), 
    panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), 
    axis.line = element_line(color=NA),
    axis.ticks = element_blank(),
    axis.text.x = element_blank(),
    axis.title.x = element_blank(),
    axis.text.y = element_text(size=14),
    axis.title.y = element_blank())


标签: r plot ggplot2