R 在x轴上使用因子变量绘制直线图

R 在x轴上使用因子变量绘制直线图,r,plot,ggplot2,R,Plot,Ggplot2,我未能在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, uc

我未能在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)
使用numeric month(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))

使用ggplot2,如果您首先以以下方式融化数据,则会更容易:

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


请注意,图表不正确,因为您只使用月份名称的第一个字母。

您的数据有问题,因为如果您只使用月份的第一个字母,您无法区分一月、六月和七月……例如……谢谢朱巴。我将月份名称改为缩写月份,效果很好。
df <- melt(df, id.vars=c("month","nmonth"))
ggplot(data=df, aes(x=month, y=value, group=variable)) + geom_line(aes(color=variable))