【问题标题】:how to prevent axes from intersecting in ggplot2如何防止轴在ggplot2中相交
【发布时间】:2013-05-01 21:00:42
【问题描述】:

我正在使用 ggplot2 制作一些对数转换数据的折线图,这些数据都具有较大的值(在 10^6 和 10^8 之间);由于轴不是从零开始,我不希望它们在“原点”相交。

这是当前坐标轴的样子:

我更喜欢从基本图形中获得的东西(但我还在 ggplot2 中使用geom_ribbon 和我真正喜欢的其他花哨的东西,所以我更愿意找到一个 ggplot2 解决方案):

这是我目前正在做的事情:

mydata <- data.frame(Day = rep(1:8, 3), 
  Treatment = rep(c("A", "B", "C"), each=8), 
  Value = c(7.415929, 7.200486, 7.040555, 7.096490, 7.056413, 7.143981, 7.429724, 7.332760, 7.643673, 7.303994, 7.343151, 6.923636, 6.923478, 7.249170, 7.513370, 7.438630, 7.209895, 7.000063, 7.160154, 6.677734, 7.026307, 6.830495, 6.863329, 7.319219))

ggplot(mydata, aes(x=Day, y=Value, group=Treatment)) 
  + theme_classic() 
  + geom_line(aes(color = Treatment), size=1) 
  + scale_y_continuous(labels = math_format(10^.x)) 
  + coord_cartesian(ylim = c(6.4, 7.75), xlim=c(0.5, 8))

plot(mydata$Day, mydata$Value, frame.plot = F)  #non-intersecting axes

【问题讨论】:

  • 所以你想要更像底部情节的东西?你可以试试geom_pointqplot

标签: r ggplot2 axes


【解决方案1】:

解决此问题的方法是使用theme(axis.line=element_blank()) 删除轴线,然后使用geom_segment() 添加虚假轴线 - 一条用于 x 轴,第二条用于 y 轴。 xyxendyend 值由您的绘图(取为绘图上显示的每个对应轴的最小值和最大值)和 coord_cartesian() 中使用的轴限制(最小值限制以确保该段被绘制在轴的位置)。

ggplot(mydata, aes(x=Day, y=Value, group=Treatment)) +theme_classic() +
 geom_line(aes(color = Treatment), size=1) +
 scale_y_continuous(labels = math_format(10^.x))+ 
 coord_cartesian(ylim = c(6.4, 7.75), xlim=c(0.5, 8))+
 theme(axis.line=element_blank())+
 geom_segment(x=2,xend=8,y=6.4,yend=6.4)+
 geom_segment(x=0.5,xend=0.5,y=6.5,yend=7.75)

【讨论】:

    猜你喜欢
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    相关资源
    最近更新 更多