【发布时间】: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