Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot:在扩展x比例限制后缩写水平网格线_R - Fatal编程技术网

R ggplot:在扩展x比例限制后缩写水平网格线

R ggplot:在扩展x比例限制后缩写水平网格线,r,R,在扩展水平轴的范围后,如何缩短ggplot的水平网格线?我正在扩展范围以显示geom_文本元素 我想使中的水平网格线在x>=2014后消失。我正在使用geom_文本标记其中一条geom_线。这是可能的,还是扩大射程一开始就错了 以下是我创建绘图的方式: library(quantmod) library(plyr) library(ggplot2) abblist <- list(CA="California",IL="Illinois",TX="Texas",NY="New York"

在扩展水平轴的范围后,如何缩短ggplot的水平网格线?我正在扩展范围以显示geom_文本元素

我想使中的水平网格线在x>=2014后消失。我正在使用geom_文本标记其中一条geom_线。这是可能的,还是扩大射程一开始就错了

以下是我创建绘图的方式:

library(quantmod)
library(plyr)
library(ggplot2)

abblist <- list(CA="California",IL="Illinois",TX="Texas",NY="New York")

cleandata <- function(thelist,i) {
    abbname <- names(thelist)[[i]]
    fullname <- thelist[[i]]
    seriesname <- paste(abbname, "UR", sep = "")
    df <- apply.yearly(getSymbols(seriesname,src='FRED',auto.assign=F),mean)
    df <- data.frame(year=time(df),coredata(df))
    df$year <- as.numeric(format(df$year, "%Y"))
    names(df)[names(df)==seriesname] <- "urate"
    df$state <- as.factor(fullname)
    df
}

urates <- rbind.fill(lapply(seq_along(abblist), cleandata, thelist=abblist))

mytheme <- theme_grey() +
theme(
    panel.background = element_rect(fill="white"),
    legend.position="none",
    axis.title.x = element_blank(),
    axis.title.y = element_blank()
)

p <- ggplot(urates, aes(x=year, y=urate)) +
geom_line(data=subset(urates,state!="New York"), aes(group=state), color="grey") +
geom_line(data=subset(urates,state=="New York"), color="maroon") +
geom_text(data=subset(urates,year==max(year) & state == "New York"), aes(label=state,x=year+0.25), hjust=0, color="grey20", size=3) + 
mytheme +
scale_x_continuous(limits=c(2000,2015),breaks=seq(2000,2014,2),minor_breaks=seq(2001,2013,2))
库(quantmod)
图书馆(plyr)
图书馆(GG2)
abblist借用一些想法,这里有一些可能对你有用的东西

首先计算NY标签的值

ny.year <- max(urates$year)
ny.val <- urates$urate[urates$year==ny.year & urates$state=="New York"]
ny.year <- ny.year +.25 # offset padding

ny.year谢谢,这太棒了——我不知道注释\自定义()或剪辑选项。要让代码一下子运行,需要在计算未调整的ny.val后,将填充添加到ny.year。此外,在textGrob的gpar中,我需要将
颜色更改为
col
,将
size
更改为
fontsize
@user1462014优点。很抱歉,我试图优化,因为我是张贴,并没有完全测试。我已经根据您的评论更新了代码。
mytheme <- theme_grey() +
theme(
    panel.background = element_rect(fill="white"),
    legend.position="none",
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    plot.margin = unit(c(1,5,1,1), "lines")
)
p <- ggplot(urates, aes(x=year, y=urate)) +
geom_line(data=subset(urates,state!="New York"), aes(group=state), color="grey") +
geom_line(data=subset(urates,state=="New York"), color="maroon") +
mytheme +
scale_x_continuous(expand=c(0,0), limits=c(2000,2014),
    breaks=seq(2000,2014,2), minor_breaks=seq(2001,2013,2))+
annotation_custom(
    grob = textGrob(label = "New York", hjust = 0, gp=gpar(col="grey20", fontsize=3)),
    ymin = ny.val, ymax = ny.val,
    xmin = ny.year, xmax = ny.year)
library(grid)
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)