向基于极坐标的ggplot2图添加第二个标题

Twi*_*ity 5 r ggplot2

我被要求使用来重新创建饼图ggplot2,并且很难在绘图中添加第二个标题。我需要在图的左下方右下方添加标题。

我当前的方法可以通过使用hjust标题放置选项来获得其中一种(0表示左对齐; 1表示右对齐):

library(ggplot2)
dat <- data.frame(variable = c("V1", "V2", "V3"),
                  value = c(.80,.50,.63))
p1 <- ggplot(dat, 
             aes(x = 1, y = value, fill = variable)) +
  geom_bar(stat = "identity") +
  coord_polar(theta = "y")  +
  theme(legend.position = 'none',
        plot.caption = element_text(hjust = 1)) +
  labs(caption  = "RIGHT CAPTION")

print(p1)
Run Code Online (Sandbox Code Playgroud)

这将产生:

带有右标题的饼图

我已经看到了一些可以使用的方法,annotate()但似乎无法使它们一起使用coord_polar()

有谁知道我如何获得第二个字幕显示在图的左侧(与右侧字幕水平对齐)?也许可以覆盖仅具有左标题的空白层?

Mar*_*dri 5

使用该grid包可以添加包含左侧标题的文本组。

library(ggplot2)
library(grid)
dat <- data.frame(variable=c("V1", "V2", "V3"), value=c(.80,.50,.63))

p1 <- ggplot(dat, aes(x = 1, y = value, fill = variable)) +
  geom_bar(stat = "identity") +
  coord_polar(theta = "y") +
  theme(legend.position='none', plot.caption=element_text(hjust=1)) +
  labs(caption="RIGHT CAPTION")

# Generate a ggplot2 plot grob
p2 <- ggplotGrob(p1)
# Find the grob tree containing the right caption (as child)
k <- which(p2$layout$name=="caption")
# Copy the "right caption" text grob in grbTxt
grbTxt <- p2$grobs[[k]]$children[[1]]

# Modify content and position of the text grob  
grbTxt$label <- "LEFT CAPTION"
grbTxt$name <- "GRID.text.left"
grbTxt$x <- unit(0,"npc")
grbTxt$hjust <- 0
grbTxt$gp$col <- "red"

# Add grbTxt (left caption) to the title grob containing the right caption
p2$grobs[[k]] <- addGrob(p2$grobs[[k]],grbTxt)
grid.draw(p2)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述