簡體   English   中英

更改X軸中斷ggplot2

[英]Change x-axis breaks ggplot2

我試圖使x軸成為實際年份,而不是ggplot2將它們分解為兩半。 如何將特定的x軸設置為年份?

dput:

df <- structure(list(year = c(1930, 1931, 1932, 1933, 1934, 1935, 1936, 
1937, 1938, 1939, 1940), ppt = c(59.0033808749924, 64.5812917950552, 
66.5926177672304, 59.4756152030825, 54.7584384173037, 73.2530022881949, 
52.152143860761, 62.667460409034, 68.3809388906197, 57.8704092161023, 
62.8957857266413), anom = c(0.820287105396386, 0.897833312663238, 
0.925795519832683, 0.826852283938926, 0.761272325064239, 1.01839068062701, 
0.725038641741477, 0.871226511754963, 0.950657430049865, 0.804536108948638, 
0.874400775857991)), row.names = c(NA, -11L), class = c("tbl_df", 
"tbl", "data.frame"), .Names = c("year", "ppt", "anom"))

碼:

qplot(year, anom, data = df, geom="smooth", main = "Precipitaion ", ylab = "Anomaly", xlab = "", scale_x_continuous(breaks=seq(1930, 1931, 1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1940), labels = c("1930", "1931", "1932", "1933", "1934", "1935", "1936", "1937", "1938", "1939", "1940")))

輸出:

在此處輸入圖片說明

該代碼應該有效,

grp = ggplot(df,aes(x=year,y=anom)) +
      geom_smooth() +
      labs(title="Precipitaion", y="Anomaly", x = "")
grp + scale_x_continuous(breaks=seq(1930, 1940, 1))

seq的語法也是seq(start,end,step-by)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM