使用r缩放水平和垂直线/刻度图(地图)

使用r缩放水平和垂直线/刻度图(地图),r,plot,ggplot2,R,Plot,Ggplot2,这里是我想要绘制的一个小示例(2个组和2个子组,只是为了简单起见,但是我可能有n个组和k个子组) grp (我还不是ggplot用户,因此这是基本图形。您可以调整ylim并添加ylab参数以正确标记。您可能还需要使用yaxt=“n”和axis()标记组和子组。) 这里有一个备用设置,使用交互值从垂直位置向量中拾取 plot(NULL, ylim=c(0.5,4.5), xlim=range(mydf$pos)) with(mydf, segments(x0=pos,

这里是我想要绘制的一个小示例(2个组和2个子组,只是为了简单起见,但是我可能有n个组和k个子组)

grp
(我还不是ggplot用户,因此这是基本图形。您可以调整
ylim
并添加
ylab
参数以正确标记。您可能还需要使用
yaxt=“n”
axis()
标记组和子组。)

这里有一个备用设置,使用交互值从垂直位置向量中拾取

 plot(NULL, ylim=c(0.5,4.5), xlim=range(mydf$pos))
 with(mydf, segments(x0=pos, 
             y0=c(1.3,1.7,3.3,3.7)[as.numeric(interaction(mydf$grp, mydf$sgrp))]-.1, 
             y1=c(1.3,1.7,3.3,3.7)[as.numeric(interaction(mydf$grp, mydf$sgrp))]+.1, 
             col= mydf$sgrp, lty=as.numeric(mydf$grp)) )
abline(h=c(1.3,1.7,3.3,3.7) ,col= rep(1:2, each=2), lty=1:2,lwd=3)

听起来您确实在要求我们为您制作此图表。一般来说,SO旨在回答您在尝试自己编写代码时遇到的特定问题。你能告诉我们你到目前为止已经走了多远吗?@joran抱歉,如果是这样的话,我有限的r知识…请注意我可以编辑的地方…没有错误消息报告,除非我知道下一步怎么做…我想看看ggplot解决方案…非常感谢解决方案,存在一些问题,例如数据中的条/线长度(水平)以及相邻条(A和B)的分组比较..但是我将继续处理..您可以调整条(和刻度)通过适当减去
abline
中的
h
参数以及
段中的
y0
y1
参数。颜色可能由
sgrp
驱动。
dgp <- c(0, 0, 0, 0, 0,  0.15, 0.15,0.15, 0.15 ,  0, 0, 0, 0,  
0.15, 0.15, 0.15, 0.15, 0.15)
mydf$dumv <- grp + dgp
plot(mydf$pos, mydf$dumv, pch = "+", ylab = "groups", xlab = "pos")
require(ggplot2)

grp <- c(  1,   1,   1,  1, 1,  1, 1, 1, 1,  2,2, 2, 2,2, 2, 2, 2, 2)
sgrp <- c("A", "A", "A", "A", "A",  "B", "B", "B", "B" ,  "A", "A", "A", "A",
  "B", "B", "B", "B", "B")
position <- c(1.1, 2.1, 3.2, 4.1, 5.0,1.1, 2.0, 5.0, 6.2,1.0, 3.0, 4.1, 5.0,1.0,
 2.1, 3.01, 4.0, 5.02)
mydf <- data.frame (grp, sgrp, pos)
dgp <- c(0, 0, 0, 0, 0,  0.15, 0.15,0.15, 0.15 ,  0, 0, 0, 0,
0.15, 0.15, 0.15, 0.15, 0.15)
mydf$barheight <- c(0.25)
mydf$group <- grp + dgp

ggplot(mydf) +
  geom_line(aes(position, factor(group), group = factor(group)),
            size = 2, colour = "purple") +
  geom_rect(aes(y = factor(group),
                xmin = position - 0.02,
                xmax = position + 0.02,
                ymin = group - barheight/2,
                ymax = group + barheight/2))
 plot(NULL, ylim=c(0,4), xlim=range(mydf$pos))
 abline(h=1:4 ,col=1:2 )
 with(mydf, segments(x0=pos, y0=as.numeric(interaction(mydf$grp, mydf$sgrp))-.2, 
                     y1=as.numeric(interaction(mydf$grp, mydf$sgrp))+.2,
                     col= mydf$grp, lty=as.numeric(mydf$sgrp)) )
 plot(NULL, ylim=c(0.5,4.5), xlim=range(mydf$pos))
 with(mydf, segments(x0=pos, 
             y0=c(1.3,1.7,3.3,3.7)[as.numeric(interaction(mydf$grp, mydf$sgrp))]-.1, 
             y1=c(1.3,1.7,3.3,3.7)[as.numeric(interaction(mydf$grp, mydf$sgrp))]+.1, 
             col= mydf$sgrp, lty=as.numeric(mydf$grp)) )
abline(h=c(1.3,1.7,3.3,3.7) ,col= rep(1:2, each=2), lty=1:2,lwd=3)