開始使用免費開始

清理你的 ridgeline 圖

我們來把上一題的圖再進一步調整,讓它更美觀、也更容易閱讀。

為了達成這點,先透過修改 ridgeline 幾何圖層的 alpha 值,讓密度區域稍微透明,減少重疊的問題。接著,移除 ggplot 在資料極值外圍預留的多餘空間,避免左右兩側出現密度沒有內插而留下的空白帶。最後,使用 theme() 函式移除 y 軸刻度,因為密度線本身已能對應到 y 軸標籤。

ggridges 套件已為你載入。

本練習屬於課程

R 視覺化最佳實務

檢視課程

練習說明

  • geom_density_ridges()alpha 設為 0.7
  • scale_x_continuous() 中設定 expand = c(0,0)
  • theme() 函式中移除 axis.ticks.y

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

md_speeding %>% 
    mutate(day_of_week = factor(day_of_week, levels = c("Mon","Tues","Wed","Thu","Fri","Sat","Sun") )) %>% 
    ggplot(aes( x = percentage_over_limit, y = day_of_week)) + 
    # make ridgeline densities a bit see-through with alpha = 0.7
    geom_density_ridges(bandwidth = 3.5, ___) +
    # set expand values to c(0,0)
    scale_x_continuous(limits = c(0,150), ___) +
    labs(subtitle = 'Guassian kernel SD = 3.5') +
    # remove y axis ticks
    theme(___)
編輯並執行程式碼