R 在左侧的线条末端打印标签
这个问题正是基于此,它为右边提供了解决方案 我需要将标签切换到另一侧,因为我的曲线合并了,但它们会被剪裁,尽管有“关闭剪裁的代码”。 (很抱歉填充,这是控制纵横比的意外结果。) 以下是图表的代码:R 在左侧的线条末端打印标签,r,ggplot2,R,Ggplot2,这个问题正是基于此,它为右边提供了解决方案 我需要将标签切换到另一侧,因为我的曲线合并了,但它们会被剪裁,尽管有“关闭剪裁的代码”。 (很抱歉填充,这是控制纵横比的意外结果。) 以下是图表的代码: library(ggplot2) p <- ggplot(temp, aes(x=year, y=value/10^6, group=variable)) + geom_point(shape=1, size=2) + geom_line(size=1) + geom_text(data=
library(ggplot2)
p <- ggplot(temp, aes(x=year, y=value/10^6, group=variable)) + geom_point(shape=1, size=2) + geom_line(size=1) +
geom_text(data=temp[temp$year==min(temp$year),], aes(label=c("MSA 2012", "MSA en cours"), x=year-.25, y=value/10^6),
hjust = 1, size=4.6) +
scale_x_continuous(breaks=seq(2001,2015,2)) + scale_y_continuous(breaks=seq(95,105,5), position="right") +
labs(x="Année", y="Emploi (millions)") +
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_blank(),
axis.line = element_line(colour = "black"), legend.position="top", legend.direction="horizontal",
axis.text = element_text(color="black", size=13), axis.title = element_text(color="black", size=13),
aspect.ratio=.25)
# Code to turn off clipping
library(grid)
gt <- ggplotGrob(p)
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)
库(ggplot2)
p一个简单的解决方案是修改与p
绘图关联的gtable布局
# Generate a toy dataset
set.seed(123)
temp = data.frame(year=rep(2001:2015,2), value=cumsum(rnorm(30)), variable=rep(c("A","B"),each=15))
library(scales)
temp$value <- rescale(temp$value, to=c(95*10^6,105*10^6))
library(ggplot2)
p <- ggplot(temp, aes(x=year, y=value/10^6, group=variable)) + geom_point(shape=1, size=2) + geom_line(size=1) +
geom_text(data=temp[temp$year==min(temp$year),], aes(label=c("MSA 2012", "MSA en cours"), x=year-.25, y=value/10^6),
hjust = 1, size=4.6) +
scale_x_continuous(breaks=seq(2001,2015,2)) + scale_y_continuous(breaks=seq(95,105,5), position="right") +
labs(x="Année", y="Emploi (millions)") +
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(), panel.background=element_blank(),
axis.line = element_line(colour = "black"), legend.position="top", legend.direction="horizontal",
axis.text = element_text(color="black", size=13), axis.title = element_text(color="black", size=13),
aspect.ratio=.25)
library(grid)
gt <- ggplotGrob(p)
### Modify the layout of the gtable
gt$widths[[2]] <- unit(2.5, "cm")
###
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)
#生成玩具数据集
种子集(123)
温度=数据帧(年份=rep(2001:2015,2),值=cumsum(rnorm(30)),变量=rep(c(“A”,“B”),每个=15))
图书馆(比例尺)
temp$value您可以使用scale\u x_continuous(…,expand=c(0.1,0))
或类似的乘法或加法展开吗?对于这样的主题,我也只会展开绘图区域,而不是处理剪裁。增加expand
或使用expand\u limits
。请注意,您的示例对我们来说是不可复制的,因此您不太可能得到准确的答案。您也可以尝试使用directlabels
软件包,它更方便