gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:24:12 25 4
gpt4 key购买 nike

我正在完善我的图表,但在绘图区域中安装直接标签时遇到问题。 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com