r - 按 x 轴扩展绘图区域,为直接标签腾出空间

标签 r plot ggplot2 axis-labels direct-labels

我正在完善我的图表,但在绘图区域中安装直接标签时遇到问题。 A 希望删除图中 y1 和 y 轴之间的大部分区域(与下面的代码生成的类似),但保留右侧的额外区域,以便为标签。

添加+scale_x_discrete(expand=c(0,0.05))会删除两侧的额外区域,但没有为标签留下空间,而且似乎无法仅删除一侧.

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

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

非常感谢任何帮助。

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)

最佳答案

aes() 中将 x 值转换为数字,然后使用 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

关于r - 按 x 轴扩展绘图区域,为直接标签腾出空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21208835/

相关文章:

r - 使用 Melt() 不会按集合 ID 排序

正则表达式 UTM 谷歌

r - 计算满足 "less than"过滤器值序列的行数

R 正在绘制页面上的标签

r - 使字符向量的重复元素唯一,但与 make.unique() 不同

python - 使用在 python 中加权的元素制作六角形图

matlab - 使用 Matlab 在功率谱分析图上检测并标记最大峰值?

r - 使用 ggplot2、R 在圆形图上绘制因子的非重叠水平

再现 Fisher 线性判别图

r - 如何将表格添加到我的 ggplot2 输出?