R 在绘图中将日期视为数字时遇到的问题

R 在绘图中将日期视为数字时遇到的问题,r,ggplot2,R,Ggplot2,我想用数字(而不是字符)来表示年份,所以间距在情节中更能代表现实。我使用了您需要的#df$年数:连续缩放(“,中断=df$年,小中断=NULL)使用:连续缩放(“,中断=df$年,小中断=NULL)。对数据帧的引用没有包含在这个语句中。好吧,我忘记了df$,真是太傻了。谢谢。为什么在转换为数字之前要将年转换为字符?@steveb你的意思是data.frame(年=c(1991…和df$年,你需要:scale\u x_continuous(“,breaks=df$years,minor\u bre

我想用数字(而不是字符)来表示年份,所以间距在情节中更能代表现实。我使用了您需要的
#df$年数:
连续缩放(“,中断=df$年,小中断=NULL)
使用:连续缩放(“,中断=df$年,小中断=NULL)。对数据帧的引用没有包含在这个语句中。好吧,我忘记了
df$
,真是太傻了。谢谢。为什么在转换为数字之前要将年转换为字符?@steveb你的意思是
data.frame(年=c(1991…
df$年,你需要:
scale\u x_continuous(“,breaks=df$years,minor\u breaks=NULL)
使用:scale\u x_continuous(“,breaks=df$years,minor\u breaks=NULL)。对数据框的引用没有包含在这条语句中。嗯,我真傻,忘记了
df$
。谢谢。为什么在转换为数字之前要将年转换为字符?@steveb你的意思是
data.frame(years=c(1991…
df$years)
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