2013-02-14 128 views
3

我沒有繪製上使用幾個月縮寫與該碼x軸的線圖:繪圖線圖形與上x軸的因子變量

yrange<-range(c(Estimate, lcl,ucl)) 
plot(nmonth, Estimate, type = "b", pch = 20, ylim = yrange, 
xlab = "Months", ylab = expression(hat(beta) * " estimates" * " & " * " confidence " * " levels ")) 
lines(nmonth, ucl, lty = 2) 
lines(nmonth, lcl, lty = 2) 
abline(h = 0, lty = 3) 

,並用這一點。

ggplot(data=df1, aes(x=nmonth, y=Estimate)) + geom_line() + geom_point() + geom_line(size=0.1) + geom_line(aes(y = ucl)) + geom_line(aes(y = lcl)) 

使用數字月(nmonth)我可以產生一個圖,但標籤不是我想要的。 如何繪製x軸上所有月份的首字母縮寫?

的數據是這樣的一個:

structure(list(Estimate = c(0.00571942142644563, 0.0111649330056159, 
0.0143761435860972, 0.00739757934210567, 0.00110764672100624, 
0.00168566337236168, 0.00392476757483504, 0.00234423892025447, 
0.000166724737089459, -0.0014580012873366, -0.00197786373686253, 
-0.00216289530501664), se = c(0.004018593736177, 0.0040534199847734, 
0.0041113846550833, 0.00402501059422328, 0.00393358629717884, 
0.00370406599461686, 0.003796651550619, 0.00392460643968604, 
0.00376380927915926, 0.00391408378704714, 0.00388845564349082, 
0.00394365265230613), nmonth = 1:12, month = structure(c(1L, 
2L, 3L, 4L, 3L, 1L, 1L, 4L, 5L, 6L, 7L, 8L), .Label = c("J", 
"F", "M", "A", "S", "O", "N", "D"), class = "factor"), lcl = c(-0.00215702229646129, 
0.00322022983546004, 0.00631782966213393, -0.000491441422571959, 
-0.00660218242146429, -0.00557430597708737, -0.0035166694643782, 
-0.00534798970153017, -0.00721034145006269, -0.00912960550994899, 
-0.00959923679810454, -0.00989245450353666), ucl = c(0.0135958651493525, 
0.0191096361757718, 0.0224344575100605, 0.0152866001067833, 0.00881747586347677, 
0.00894563272181073, 0.0113662046140483, 0.0100364675420391, 
0.00754379092424161, 0.00621360293527579, 0.00564350932437948, 
0.00556666389350337)), .Names = c("Estimate", "se", "nmonth", 
"month", "lcl", "ucl"), class = "data.frame", row.names = c(NA, 
-12L)) 
+0

您的數據有問題,因爲如果您只收到本月的第一個字母,則無法區分一月,六月和七月...... – juba 2013-02-14 11:30:37

回答

2

隨着GGPLOT2,這是比較容易,如果你第一次融化你的數據是這樣的:

df <- melt(df, id.vars=c("month","nmonth")) 

然後你就可以直接做:

ggplot(data=df, aes(x=month, y=value, group=variable)) + geom_line(aes(color=variable)) 

enter image description here

請注意,圖表不正確,因爲您僅使用月份名稱的第一個字母。

+0

謝謝朱巴。我已將月份名稱更改爲縮寫月份,並且它工作正常。 – Meso 2013-02-14 22:54:29