2014-01-18 51 views
4

我正在打磨我的图形,并在绘图区域中拟合直接标签时出现问题。 A想要移除y1和图中左侧的y轴之间的大部分区域,类似于下面的代码所生成的区域,但将额外区域保留在右侧以为标签留出空间。用x轴扩大绘图区域,为直接标签腾出空间

添加+scale_x_discrete(expand=c(0,0.05))会在两侧删除多余的区域,但不会留下标签空间,并且似乎无法仅在一侧移除它。

使用+theme(plot.margin = unit(c(0,4,0,0), "cm"))向绘图区右侧添加边距仍然不允许标签出现在那里。

将标签放置在边界右侧的解决方案会更好。

任何帮助非常感谢。

library(ggplot2) 
library(directlabels) 
library(reshape2) 
theme_set(theme_bw()) 
# some data 
dfr<-data.frame(c("Longish Name A","Longish Name B","Longish Name C"),c(1,1,1),c(1,2,3),c(2,3,4)) 
colnames(dfr) <- c("subject","y1","y2","y3") 
dfr<-melt(dfr, id.vars="subject") 
# the graph 
ggplot(data=dfr,aes(y=value, x=variable, group=subject)) + 
geom_line(aes(color=subject))+ 
geom_dl(aes(label=subject), list(dl.trans(x=x+0.2), "last.qp", cex=0.5)) + 
guides(color=FALSE) 

回答

4

将您的x值到数字的aes()内,然后用scale_x_continuous()找回原来的标签,并设置limits=是上侧宽。

ggplot(data=dfr,aes(y=value, x=as.numeric(variable), group=subject)) + 
    geom_line(aes(color=subject))+ 
    geom_dl(aes(label=subject), list(dl.trans(x=x+0.2), "last.qp", cex=0.5)) + 
    guides(color=FALSE)+ 
    scale_x_continuous(breaks=c(1,2,3),labels=c("y1","y2","y3"),expand=c(0,0.05), 
        limits=c(1,3.4)) 

enter image description here