将点连接到r中的ggplot2中的行 [英] connecting points with lines in ggplot2 in r

查看:130
本文介绍了将点连接到r中的ggplot2中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据:

  mydata < -  data.frame(grp = c(1,1,1, 1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,2),
grp1 = c(A,A AAABBBB
AAAA B,B,B,B,B),
namef = c(M1,M3,M2,M4,M5,M1 M3,M4,
,M0,M6,M7,M8,M10,M6,M7,M8,M9, M10),
dgp = c(1,1,1,1,1,1,1.15,1.15,1.15,1.15,
2,2,2,2,2.15,2.15,2.15,2.15 ,2.15),
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, (ggplot2)

plt< - ggplot(mydata)+ geom_point(aes(position,dgp,
group =因子(dgp)),size = 2,color =purple)+
geom_text(data = mydata,aes(x = position,y = dgp + 0.05,
label = namef))

plt



我想连接点来自变量namef的ame标签。



I认为geom_segment适合处理这种情况:

pre $ code require(grid)
plt + geom_segment(aes(xend = position ,yend = dgp),
arrow = arrow(length = unit(0.1,cm)))


解决方案

geom_line 将根据组美学,所以:

pre $ g $ p $ g $ = 2,color =purple)+
geom_line()+
geom_text(data = mydata,aes(x = position,y = dgp + 0.05,label = namef))

会让你这样:
$ b



另外,通常最好把 aes()在原来的调用中调用 ggplot ,然后只添加一个如果你需要覆盖
某些美学,那么对于单个几何体来说, aes() data 参数


Here is my data:

    mydata <- data.frame (grp = c(  1,   1,   1,  1, 1,  1, 1, 1, 1, 
     2,2, 2, 2,2, 2, 2, 2, 2),
    grp1 = c("A", "A", "A", "A", "A",  "B", "B", "B", "B" ,  
    "A", "A", "A", "A",      "B", "B", "B", "B", "B"), 
   namef = c("M1", "M3", "M2", "M4", "M5","M1", "M3", "M4", 
    "M0", "M6", "M7", "M8",       "M10", "M6", "M7", "M8", "M9", "M10"),
     dgp = c(1, 1, 1, 1, 1,  1.15, 1.15,1.15, 1.15 ,
        2, 2, 2, 2,2.15, 2.15, 2.15, 2.15, 2.15), 
    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))

require(ggplot2)

plt <- ggplot(mydata) +   geom_point(aes(position, dgp, 
group = factor(dgp)),   size = 2, colour = "purple") +
geom_text(data = mydata,aes(x=position,y=dgp + 0.05,
 label=namef))

plt

I want to connect point with same label from variable namef.

I thought geom_segment is appropriate to handle the situation:

require(grid)
plt + geom_segment(aes(xend = position, yend = dgp), 
arrow = arrow(length = unit(0.1,"cm")))

解决方案

geom_line will connect points according to the group aesthetic, so:

ggplot(mydata, aes(position, dgp, group = namef)) +   
  geom_point(size = 2, colour = "purple") +
  geom_line() +
  geom_text(data = mydata,aes(x=position,y=dgp + 0.05, label=namef))

gets you this:

Also, it's generally better to put the aes() call in your original call to ggplot, and then only add an aes() or data argument to the individual geoms if you need to override some aesthetics.

这篇关于将点连接到r中的ggplot2中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆