這個問題在這里已經有了答案:
我想要 YYYY 格式的 x 軸標簽。另外,我不想將資料型別更改為字符。
附加示例資料集
structure(list(State = c("Punjab", "Manipur", "Haryana", "Karnataka",
"Punjab", "Puducherry", "Arunachal Pradesh", "Nagaland", "Chandigarh",
"Tamil Nadu"), Year = c(2001L, 2002L, 2008L, 2002L, 2004L, 2002L,
2001L, 2009L, 2005L, 2009L), Type_code = c("Causes", "Means_adopted",
"Causes", "Means_adopted", "Causes", "Causes", "Means_adopted",
"Means_adopted", "Means_adopted", "Causes"), Type = c("Cancellation/Non-Settlement of Marriage",
"By Drowning", "Dowry Dispute", "By Consuming Insecticides",
"Illness (Aids/STD)", "Cancer", "By Machine", "By Drowning",
"By Hanging", "Insanity/Mental Illness"), Gender = c("Male",
"Male", "Female", "Female", "Male", "Female", "Female", "Male",
"Male", "Female"), Age_group = c("45-59", "60 ", "0-14", "45-59",
"0-14", "60 ", "0-14", "45-59", "0-14", "15-29"), Total = c(0L,
0L, 0L, 62L, 0L, 0L, 0L, 0L, 0L, 38L)), class = "data.frame", row.names = c(NA,
-10L))
感謝您的時間 :)
uj5u.com熱心網友回復:
您可以通過指定 來控制刻度的放置位置scale_x_continuous(breaks=...)。例如,我們可以使用基數 RaxisTicks來生成一組合理的數字:
plot1 = ggplot(data = cases_over_11_years)
plot1 geom_step(aes(x = Year, y = Total), stat = "identity", size = 1, color = "red")
labs( title = "Change in numbers over the time for 11 years",
x = "Year",y = "Cases per year")
theme(axis.text = element_text(size = 18))
theme(axis.title = element_text(size = 20))
theme(plot.title = element_text(size=22))
scale_x_continuous(breaks = ~ axisTicks(., log = FALSE))
![繪圖時如何避免將年份默認轉換為小數?[復制]](https://img.uj5u.com/2022/01/10/6fe8bcf9346b488cbd2358b8497ba2fc.png)
如果這不能與您的真實資料完美配合,那么您可以隨時更新函式以僅考慮整數(可能是匆忙unique(round(axisTicks(., log = FALSE), 0)))。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/407011.html
標籤:
