我希望将年份表示为数字(而不是字符),因此间距在情节中表现得更好。我使用# df$years <- as.numeric(as.character(df$years))
,但收到错误Error in is.vector(breaks) : object 'years' not found
。我的整个代码如下:
library(ggplot2)
library(scales)
df <- data.frame(years=c(1991, 1993, 1997, 2001, 2005, 2007, 2011, 2015),
freq=c(43.20, 52.13, 47.93, 46.29, 40.57, 53.88, 48.92, 50.92))
df$years <- as.numeric(as.character(df$years))
point <- format_format(big.mark = ".", decimal.mark = ",", scientific = FALSE)
p <- (ggplot(df, aes(x=years, y=freq, label=point(round(freq,1)))) +
geom_line(size=.7, color="#999999") +
geom_point(size=3, color="black") +
geom_text(vjust=c(2, -1, -1.5*sign(diff(diff(df$freq))) + 0.5)) +
theme_bw() +
theme(panel.border=element_blank(), panel.grid.minor=element_blank(),
axis.title.y=element_text(vjust=1.25)) +
scale_x_continuous("", breaks=years, minor_breaks=NULL) +
scale_y_continuous("", limits=c(0, 60),
breaks=seq(0, 60, 10), minor_breaks=NULL))
p