完善ggplot R代码,使带注释的矩形出现在绘图点后面 [英] Refining ggplot R code so annotated rectangle appears behind plot points
问题描述
使用以下数据,我试图为演示文稿创建图形:
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())
这是上面代码的结果:
我遇到的问题是矩形正在逐渐淡化绘图点的颜色.如何使灰色矩形出现在绘图点和值标签的后面?
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.
推荐答案
尝试使用geom_rect而不是注释.然后,您将要从aes函数中删除美学.我不能说为什么会产生错误,但是会产生错误.
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())
这篇关于完善ggplot R代码,使带注释的矩形出现在绘图点后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!