R 如何使用ggplot将轴元素放入框中

R 如何使用ggplot将轴元素放入框中,r,ggplot2,R,Ggplot2,我搜索了我的问题,但显然,没有关于这个的话题。任何帮助都将不胜感激 假设我有一个由以下内容生成的绘图: library(tidyverse) df<- data.frame(PCP = c("BOB","FRED","ED","Other"), closed = c(42,64,45,1812), total= c(53,81,58,3188)

我搜索了我的问题,但显然,没有关于这个的话题。任何帮助都将不胜感激

假设我有一个由以下内容生成的绘图:

library(tidyverse)

df<- data.frame(PCP = c("BOB","FRED","ED","Other"),
                closed = c(42,64,45,1812), 
                total= c(53,81,58,3188), 
                percentage = c(.7924,.7901,.7758,.5683),
                row= c(1, 2, 3,4),
                color =c("0099FF","#CCCCCC","#CCCCCC","#660033"),
                color_fill = c("99","0","0","98"
                ))

col  <- c(
  "99" = "#0099FF",
  "98" = "#660033", 
  "0" = "#CCCCCC" 
)

df %>%  
  arrange(desc(percentage)) %>% 
  mutate(PCP = PCP,
         closed = closed,
         total = total,
         percentage = percentage,
         row = row,
         color = color,
         color_fill = color_fill) -> df1

ggplot(df1,aes(x=PCP, y = percentage,fill=color_fill, color = color)) +
  geom_col() +
  coord_flip() +
  labs(x ="PCP Name",
       y = "Percentage of Gap Closures",
       title =  "TOP 10 PCPs")+
  scale_fill_manual(values = col)+
  scale_color_manual(values = col) +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        panel.background = element_blank(),
        text = element_text(size=15),
        plot.caption = element_text(hjust = 0, face= "italic"),
        axis.text.y = element_text(colour = col ))

库(tidyverse)
df%
突变(PCP=PCP,
关闭,
总计=总计,
百分比=百分比,
行=行,
颜色=颜色,
颜色填充=颜色填充)->df1
ggplot(df1,aes(x=PCP,y=百分比,填充=color\U填充,color=color))+
geom_col()+
coord_flip()+
实验室(x=“PCP名称”,
y=“间隙闭合的百分比”,
title=“前10名PCP”)+
刻度\填充\手动(值=列)+
比例\颜色\手册(值=列)+
主题(legend.position=“无”,
panel.grid=element\u blank(),
panel.background=元素_blank(),
文本=元素\文本(大小=15),
plot.caption=element\u text(hjust=0,face=“italic”),
axis.text.y=元素\文本(颜色=列))

如何将
0.6
放在彩色框内的x轴上,将
FRED
放在y轴上?

使用两个
注释来查看这里(注意
coord_filp()
已更新)


如果您坚持使用“框”,那么对于
注释

可能使用
而不是
rect
,请首先查看此处:不幸的是,这没有帮助,因为我的标签在两个轴上都不同。
ggplot(df1,aes(x=PCP, y = percentage,fill=color_fill, color = color)) +
  geom_col() +
  #coord_flip() +
  labs(x ="PCP Name",
       y = "Percentage of Gap Closures",
       title =  "TOP 10 PCPs")+
  scale_fill_manual(values = col)+
  scale_color_manual(values = col) +
  theme(legend.position = "none",
        panel.grid = element_blank(),
        panel.background = element_blank(),
        text = element_text(size=15),
        plot.caption = element_text(hjust = 0, face= "italic"),
        axis.text.y = element_text(colour = col )) +
  annotate("rect", xmin = 2.8, xmax = 3.2, ymin = -0.091, ymax = -0.045, alpha=0.5, fill="red") +
  annotate("rect", xmin = 0.2, xmax = 0.35, ymin = 0.58, ymax = 0.62, alpha=0.5, fill="red") +
  coord_flip(xlim = c(1,4), ylim = c(0, 0.8), clip = "off")