R ggplot将注释放入geom_rect

时间:2019-03-19 01:18:57

标签: r ggplot2

我有geom_tile的极坐标图。我想在中间放置一个灰色圆圈,并在其中放置图表标题。

这是我可复制的实现

require(ggplot2)
# Create the data frame.
sales_data <- data.frame(
  emp_name = c("Sam", "Dave", "John", "Harry", "Clark", "Kent", "Kenneth", "Richard", "Clement", "Toby", "Jonathan"), 
  month = as.factor(c("Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan", "Jan")),
  dept_name = as.factor(c("Production", "Services", "Support", "Support", "Services", "Production", "Production", "Support", "Support", "Support", "Production")), 
  revenue = c(100, 200, 300, 400, 500, 600, 500, 400, 300, 200, 500)
)

sales_data$month <- factor(sales_data$month, levels = c("Jan", "Feb", "Mar"))


categorical_bubble_chart <- ggplot(data = sales_data, aes(x = month, y = dept_name)) +
  #geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -1, ymax = 0), fill = "grey", alpha = 0.03)+
  #annotate("text", x=0, y=-1, label= "Risk Register", fontface =2) +
  geom_tile(data = expand.grid(sales_data$month, sales_data$dept_name), 
            aes(x = Var1, y = Var2), fill = NA, col = 'gray50', lty = 1) +
  geom_point(aes(size = revenue, col = revenue), 
             shape = 16, position = position_jitter(seed = 0), show.legend = F) +
  geom_text(aes(label = revenue), vjust = 1.6, position = position_jitter(seed = 0)) +

  geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -1, ymax = 0), fill = "grey", alpha = 0.03)+
  annotate("text", x=0, y=-1, label= "Chart title", fontface =2) +

  theme_bw() +
  theme(
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.background = element_blank(), 
    axis.line = element_blank(), 
    panel.border = element_blank(), 
    panel.grid = element_blank()
  ) +
coord_polar()

categorical_bubble_chart

输出: enter image description here

如果注释最后一行coord_polar()以将其绘制在笛卡尔坐标中。这是输出: enter image description here

因此,基本上,灰色矩形应位于底部瓷砖行的正下方,以便在极坐标中显示为适合内部的圆且没有断层。

请帮助我或建议一个方向。

1 个答案:

答案 0 :(得分:1)

有几件事情可以使这项工作。首先,如果将x轴(月)转换为数字而不是因子,则更加容易。我们可以使用scale_x_continuous更多地控制轴。完成此操作后,您只需要设置geom_rect坐标以填充该空间:

sales_data$month <- as.integer(sales_data$month)

ggplot(sales_data, aes(x = month, y = dept_name)) +
  geom_tile(data = expand.grid(sales_data$month, sales_data$dept_name), 
            aes(x = Var1, y = Var2), fill = NA, col = 'gray50', lty = 1) +
  geom_point(aes(size = revenue, col = revenue), 
             shape = 16, position = position_jitter(seed = 0), show.legend = F) +
  geom_text(aes(label = revenue), vjust = 1.6, position = position_jitter(seed = 0)) +

  geom_rect(aes(xmin = 0.5, xmax = 3.5, ymin = -1, ymax = 0.5), fill = "grey", alpha = 0.03)+
  annotate("text", x=0.5, y=-1, label= "Chart title", fontface =2) +
  theme_bw() +
  theme(
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.background = element_blank(), 
    axis.line = element_blank(), 
    panel.border = element_blank(), 
    panel.grid = element_blank()
  ) +
  coord_polar(start = 0.5, clip = 'off') +
  scale_x_continuous(limits=c(0.5,3.5), expand = c(0,0), breaks = 1:3, labels = c('Jan', 'Feb', 'Mar'))

enter image description here