[英]Rename tick marks on x-axis in spaghetti plot using ggplot2
我對R很陌生,我試圖創建一個意大利面條圖,以比較4組在不同時間點(手術后1小時,手術后4小時,8小時和術后1天早上和之后,術后第1天下午)。
玩了之后,我從以下代碼開始創建了一個不錯的圖形:
plot <-- ggplot(data=rescue, aes(Measurement, Percentage, color=factor(Group))) + geom_line()
通過幾個命令,我根據自己的喜好調整了圖形,並得到了以下結果:
我想用標簽1 hour
, 4 hours
, 8 hours
, POD1 Morning
和POD1 Afternoon
替換x-axis
的1 hour
。
我搜索了該網站和google,並想到了以下代碼:
plot1 <-- plot + scale_x_discrete(labels=c("1"="1 hour", "2"="4 hours", "3"="8 hours", "4"="POD1 Morning", "5"="POD1 Afternoon"))
我也嘗試了levels=c(1,2,3,4,5)
和labels=c("1 hour", "4 hours", etc)
但是我得到的只是一個空的x-axis
,沒有新的標簽。 我似乎無法使其工作。 在另一篇文章中,我閱讀了使用xaxt="n"
的建議,但無法將其與ggplot2
一起ggplot2
。
誰能幫我嗎? 非常感謝!
考慮在scale_x_discrete()
使用中斷
plot <- ggplot(data=rescue, aes(Measurement, Percentage, color=factor(Group))) +
geom_line() +
scale_x_discrete(breaks=c(1, 2, 3, 4, 5),
labels=c("1 hour", "4 hours", "8 Hours", "POD1 Morning", "POD1 Afternoon"))
您正在嘗試將離散軸應用於連續變量(測量)。 解決方法是將Measurement變成因素:
plot <-- ggplot(data=rescue, aes(factor(Measurement), Percentage, color=factor(Group))) + geom_line()
您可能需要通過添加手動更改標簽
+ labs(x = 'Measurement')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.