R 在ggplot geom_路径中添加轻微曲线(或弯曲),使路径更易于读取

R 在ggplot geom_路径中添加轻微曲线(或弯曲),使路径更易于读取,r,plot,ggplot2,R,Plot,Ggplot2,此问题是先前回答的问题中的新问题,位于此处: 正如您在下面的.jpg图片中所看到的——红线geom_路径被挤在一起,使得这条线更难解释。是否有办法稍微“弯曲”曲线,以减少重叠/聚束?围绕点进行某种平滑或弯曲,使直线不重叠? 以下是我的语法: orbit.plot <- ggplot(orbit.data, aes(x=OpM, y=INVT, colour=Subj, label=Year)) + geom_point(size=7, shape=20) + geom_path

此问题是先前回答的问题中的新问题,位于此处:

正如您在下面的.jpg图片中所看到的——红线geom_路径被挤在一起,使得这条线更难解释。是否有办法稍微“弯曲”曲线,以减少重叠/聚束?围绕点进行某种平滑或弯曲,使直线不重叠?

以下是我的语法:

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))
谢谢你

编辑:更新:本质上,此绘图的原因是显示随时间变化的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)
库(ggplot2)

orbit.data对帖子有用吗?有时我认为人们之所以对帖子投反对票,仅仅是因为他们对自己无法解决这些问题感到恼火。你希望你的情节传达什么信息?Richo64:感谢你的链接和建议——在我仔细研究之后,这些信息可能会有所帮助——再次感谢。样条线有用吗?迈克——如果你愿意的话倾向于在月光下做一些R咨询工作(!)你能告诉我如何与你联系吗。非常感谢。CBMikeWise1618 at gmail:)
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)