【问题标题】:Adding Slight curve (or bend) in ggplot geom_path to make path easier to read在 ggplot geom_path 中添加轻微曲线(或弯曲)以使路径更易于阅读
【发布时间】:2015-12-26 16:48:30
【问题描述】:

这个问题是以前回答的问题中的一个新问题:Plot mean of data within same ggplot

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

这是我的语法:

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) 位小数 - 因此数据的“轻微”弯曲不会污染数据试图描绘的“本质”。

【问题讨论】:

  • this 的帖子有用吗?
  • 有时我认为人们对帖子投反对票仅仅是因为他们对无法解决问题感到恼火。
  • 你希望你的情节传达什么信息?
  • Richo64:感谢您提供的链接和建议——在我仔细检查之后,这些信息可能会有所帮助——再次感谢。
  • 样条线有帮助吗?

标签: r plot ggplot2


【解决方案1】:

你可以对它进行样条化:

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)

这给出了:

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

【讨论】:

  • Mike——如果你想通过月光(!)做一些 R 咨询工作,你能告诉我如何离线联系你吗?谢谢你。 CB
  • MikeWise1618 at gmail :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
  • 2021-12-29
  • 2017-03-19
相关资源
最近更新 更多