关于ggplot2:旋转geom_point形状并在该行上方显示geom_text

时间:2018-08-15 07:10:40

标签: r ggplot2 plot rstudio visualization

早上好。

我正在尝试使用ggplot2软件包进行绘图,但是面临以下问题:

为了使其更易于理解,这是我要制作的目标图像。

enter image description here

就像图片一样,我想做以下事情:

1)在虚线上方放置一个文本“ median”,以便可以清楚地看到该字符。

2)旋转三角形的度数(不是^ ^而是<>),使之有意义。

为达到上述目的,到目前为止,我已经完成了以下代码:

# binding the data, defining the x and y aesthetics, title, labels
w_plot <- ggplot(
  data = com_mal, 
  aes(x = reorder(name, -median_age),  y = median_age)
)

labels = c('5 yrs old', 10, 15, 20, 25, 30)

w_plot + 
  geom_linerange(
    aes(ymin = q1_age, ymax = q3_age),
    color = "#76bde0", 
    size = 6, 
    alpha = 0.7
  ) + 
  geom_point(fill = "#ed3324", colour = "white", size = 4, shape = 21) +
  geom_text(aes(y = 9, x = 15, label = '25th')) +
  geom_text(aes(y = 20, x = 15, label = '75th percentile')) +
  geom_text(aes(y = 30, x = 22, label = 'median')) +
  geom_point(aes(y = 8.25, x = 15), shape = 17) +
  geom_point(aes(y = 21.75, x = 15), shape = 17) +
  geom_point(aes(y = 29, x = 21.9), fill = "#ed3324", colour = "white", size = 4, shape = 21) +
  geom_hline(aes(yintercept = 5), linetype = 'dotted') +
  geom_hline(aes(yintercept = 10), linetype = 'dotted') +
  geom_hline(aes(yintercept = 15), linetype = 'dotted') +
  geom_hline(aes(yintercept = 20), linetype = 'dotted') +
  geom_hline(aes(yintercept = 25), linetype = 'dotted') +
  geom_hline(aes(yintercept = 30), linetype = 'dotted') +
  scale_y_continuous(breaks = seq(5, 30, by = 5), position = 'right', labels = labels) +
  coord_flip() +
  labs(title = 'Youngest Male Names',
       subtitle = 'By estimated median age for Americans alive as of Jan 1. 2014',
       x = NULL, y = NULL, caption =  'SOURCE: SOCIAL SECURITY ADMINISTRATION') +
  theme(plot.title = element_text(face = 'bold', size = 16),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.ticks = element_blank(), plot.caption = element_text(size = 10))

enter image description here

非常感谢您!

1 个答案:

答案 0 :(得分:3)

对于三角形,您可以改用geom_text(),将family参数设置为支持字符的字体,对于标签,请使用geom_label()

 geom_text(label = "▶", size = 3, family = "HiraKakuPro-W3")
 geom_label(aes(y = 4, x = 10, label = 'median'), fill = "grey92", label.size = NA)

label.size删除了标签的轮廓,“ grey92”是(大约?)背景的颜色。

如果您希望虚线位于标签后面,则应在该行之后的 中将geom_label()添加到绘图中。 (还要注意,您可以在同一行代码中添加所有虚线。)

w_plot + 
  geom_linerange(
    aes(ymin = q1_age, ymax = q3_age),
    color = "#76bde0", 
    size = 6, 
    alpha = 0.7
  ) + 
  geom_point(fill = "#ed3324", colour = "white", size = 4, shape = 21) +
  geom_text(aes(y = 9, x = 15, label = '25th')) +
  geom_text(aes(y = 20, x = 15, label = '75th percentile')) +
  geom_text(aes(y = 8.25, x = 15),label = "◀", size = 3, 
            family = "HiraKakuPro-W3") +
  geom_text(aes(y = 21.75, x = 15),label = "▶", size = 3, 
            family = "HiraKakuPro-W3") +
  geom_point(aes(y = 29, x = 21.9), fill = "#ed3324", colour = "white", 
             size = 4, shape = 21) +
  geom_hline(yintercept = seq(5, 30, by = 5), linetype = 'dotted') +
  geom_label(aes(y = 30, x = 22, label = 'median'), 
             fill = "grey92", label.size = NA) +
  scale_y_continuous(breaks = seq(5, 30, by = 5), 
                     position = 'right', labels = labels) +
  coord_flip() +
  labs(title = 'Youngest Male Names',
       subtitle = 'By estimated median age for Americans alive as of Jan 1. 2014',
       caption =  'SOURCE: SOCIAL SECURITY ADMINISTRATION') +
  theme(plot.title = element_text(face = 'bold', size = 16),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.ticks = element_blank(), plot.caption = element_text(size = 10))