【问题标题】:About ggplot2: Rotate geom_point shape & Show geom_text above the line关于 ggplot2:旋转 geom_point 形状并在线上方显示 geom_text
【发布时间】:2019-01-22 01:46:43
【问题描述】:

早安。

我正在尝试使用 ggplot2 包进行绘图,但遇到以下问题:

为了更容易理解,这里是我想要制作的目标图片。

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

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))

非常感谢!

【问题讨论】:

  • 请添加一些数据。所以我们可以重现情节。而不是geom_text 尝试geom_label()
  • 而不是geom_point,使用geom_polygon,你事先已经计算了两个三角形每个角的位置。您示例中的 y 坐标以 y = 11 为中心(如果我计算正确的话)。
  • 感谢您的所有回答! :)

标签: r ggplot2 plot rstudio visualization


【解决方案1】:

对于三角形,您可以改用 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))

【讨论】:

  • @supremed14:您能否将dput(com_mal) 的输出添加到您的问题中,以便其他人可以重现该情节?还请将 Luca 的帖子标记为答案,以帮助未来的读者。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多