gpt4 book ai didi

r - 在 ggplot geom_path 中添加轻微的曲线(或弯曲)以使路径更易于阅读

转载 作者:行者123 更新时间:2023-12-05 04:14:43 25 4
gpt4 key购买 nike

这个问题是来自以前回答的问题的新问题,位于:Plot mean of data within same ggplot

正如您在下面的 .jpg 图片中看到的那样——红线 geom_path 被挤压在一起,使这条线更难理解。有没有办法稍微“弯曲”曲线以减少重叠/聚束?在点周围进行某种平滑或弯曲以使线条不重叠? Red-line geom_paths are squeezed together

这是我的语法:

orbit.plot <- ggplot(orbit.data, aes(x=OpM, y=INVT, colour=Subj, label=Year)) +
geom_point(size=7, shape=20) +
geom_path(size=1.5) +
ggtitle("Title Orbits") +
geom_text(data=subset(orbit.data,Year==2006 | Year==2014), aes(label=Year, vjust=1, hjust=1)) +
theme(panel.background = element_rect(fill = 'white', colour = 'red'),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
geom_vline(xintercept=0, size=1) +
geom_hline(yintercept=7, size=1) +
scale_y_continuous(limits = c(7, 15), breaks=seq(7,15,1/2))

这里是数据集的输入:

structure(list(Year = c(2006L, 2006L, 2007L, 2007L, 2008L, 2008L, 
2009L, 2009L, 2010L, 2010L, 2011L, 2011L, 2012L, 2012L, 2013L,
2013L, 2014L, 2014L), Subj = structure(c(2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("TMC",
"HMC"), class = "factor"), OPM = c(0.088, 0.09, 0.095, 0.078,
0.085, 0.08, -0.023, 0.019, 0.009, 0.043, 0.025, 0.065, 0.0199,
0.029, 0.06, 0.055, 0.088, 0.065), Invt = c(14.5, 10.3, 13.8,
10, 13.3, 9.5, 12.3, 8, 13.5, 8, 14.3, 10, 13.2, 8.5, 13.8, 9.5,
13.8, 9.75)), .Names = c("Year", "Subj", "OpM", "INVT"
), class = "data.frame", row.names = c(NA, -18L))

谢谢你。

编辑:更新:本质上,这个图的原因是显示 x/y 变量随时间的“运动”。在 X 轴上——我正在绘制一个比率(在本例中为营业利润率)。在 Y 轴上——我展示了一个周期度量(在这种情况下是库存周转。)曲线的“弯曲”肯定会“弯曲”数据本身——但是我使用的是 X/Y 度量,数据被理解为二 (2) 位小数——因此数据的“轻微”弯曲不会污染数据试图描绘的“本质”。

最佳答案

你可以给它加样条:

library(ggplot2)
orbit.data <- structure(list(Year =
c(2006L, 2006L, 2007L, 2007L, 2008L, 2008L, 2009L, 2009L, 2010L, 2010L,
2011L, 2011L, 2012L, 2012L, 2013L, 2013L, 2014L, 2014L),
Subj = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L),
.Label = c("TMC", "HMC"), class = "factor"),
OPM = c(0.088, 0.09, 0.095, 0.078, 0.085, 0.08, -0.023, 0.019, 0.009,
0.043, 0.025, 0.065, 0.0199, 0.029, 0.06, 0.055, 0.088, 0.065),
Invt = c(14.5, 10.3, 13.8, 10, 13.3, 9.5, 12.3, 8, 13.5, 8, 14.3,
10, 13.2, 8.5, 13.8, 9.5, 13.8, 9.75)),
.Names = c("Year", "Subj", "OpM", "INVT"), class = "data.frame",
row.names = c(NA, -18L))

lsdf <- list()
plot.new()
for (f in unique(orbit.data$Subj)){
psdf <- orbit.data[orbit.data$Subj==f,]
newf <- sprintf("%s - xspline",f)
lsdf[[f]] <- data.frame(xspline(psdf[,c(3:4)], shape=-0.6, draw=F),Subj=newf)
}
sdf <- do.call(rbind,lsdf)
orbit.plot <- ggplot(orbit.data, aes(x=OpM, y=INVT, colour=Subj, label=Year)) +
geom_point(size=5, shape=20) +
geom_point(data=orbit.data,size=7, shape=20,color="black") +
geom_path(size=1) +
geom_path(data=sdf,aes(x=x,y=y,label="",color=Subj),size=1) +
ggtitle("Title Orbits") +
geom_text(data=subset(orbit.data,Year==2006 | Year==2014),
aes(label=Year, vjust=1, hjust=1)) +
theme(panel.background = element_rect(fill = 'white', colour = 'red'),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
geom_vline(xintercept=0, size=1) +
geom_hline(yintercept=7, size=1) +
scale_y_continuous(limits = c(7, 15), breaks=seq(7,15,1/2))
print(orbit.plot)

给出:

enter image description here

有很多方法可以做到这一点,我怀疑这是最好的。您可以在 xspline 调用中使用 shape 参数来获得不同的曲率。

关于r - 在 ggplot geom_path 中添加轻微的曲线(或弯曲)以使路径更易于阅读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34473292/

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