修正不美觀的標籤位置
這些標籤在點圖中的線條上仍有些重疊。請使用有條件的 hjust 美學屬性來更好地放置它們,並調整其外觀。
本練習屬於課程
在 Tidyverse 中以資料溝通
練習說明
- 在
geom_text()函式呼叫中,若為年份"2006"的標籤,將hjust美學屬性設為1.4,否則設為-0.4。請使用ifelse()函式來完成。 - 同樣在
geom_text()呼叫中,將字型大小、字型家族與顏色分別設為3、"Bookman"與"gray25",但要放在aes()函式之外,因為這些值不是由資料驅動的。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Save plot into an object for reuse
ilo_dot_plot <- ggplot(ilo_data) +
geom_path(aes(x = working_hours, y = country),
arrow = arrow(length = unit(1.5, "mm"), type = "closed")) +
# Specify the hjust aesthetic with a conditional value
geom_text(
aes(x = working_hours,
y = country,
label = round(working_hours, 1),
hjust = ifelse(___)
),
# Change the appearance of the text
___ = ___,
___ = "___",
___ = "___"
)
ilo_dot_plot