gpt4 book ai didi

r - 关于 ggplot2 : Rotate geom_point shape & Show geom_text above the line

转载 作者:行者123 更新时间:2023-12-04 01:53:30 27 4
gpt4 key购买 nike

早上好。

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

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

enter image description here

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

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

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

非常感谢!

最佳答案

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

关于r - 关于 ggplot2 : Rotate geom_point shape & Show geom_text above the line,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51854352/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com