按类别创建散点图,其中的行连接ggplot

时间:2017-02-07 22:50:37

标签: r ggplot2

我正在尝试根据两个类别创建一个散点图,并用一条线连接任何具有相同ID的点。我已经创建了一个示例数据集。我希望有几天成为x轴,得分为y轴,每个值的研究都有不同的颜色。正如我所示,我可以在ggplot中做到这一点,但我无法弄清楚如何连接来自同一ID的点。

library(ggplot2)
Id <- c(1,2,3,4,1,3)
Score <- c(24,27,17,25,28,24)
Study <- c(1,1,1,2,2,2)
days <- c(8,17,42,36,69,80)

X <- as.data.frame(cbind(Id, Score, Study, days ))

ggplot(X, aes(days, Score)) + 
geom_jitter(width = 0.3, height = 0.3, aes(colour = as.factor(Study))) +
geom_segment(aes(x=42, y=17, xend=80, yend=24), lty=2, colour="purple") +
geom_segment(aes(x=8, y=24, xend=69, yend=28), lty=2, colour="purple") 

enter image description here

例如,我希望在ID 1的值24和28之间存在一条线,并且ID 3的值在17到24之间。我已经使用geom_segment添加了这些,但在实际数据集中我是与他们的意志一起工作将有数百个连接。感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:3)

使用$query = $this->CI->db->get($this->sess_table_name); //Multiple AJAX calls checking //But adding add a loop to check a couple more times has stopped premature session breaking $counter = 0; while( !$query && $counter < $this->sess_query_attempts ){ usleep(100000);//wait a tenth of a second $this->CI->db->where('session_id', $session['session_id']); if ($this->sess_match_ip == TRUE) { $this->CI->db->where('ip_address', $session['ip_address']); } if ($this->sess_match_useragent == TRUE) { $this->CI->db->where('user_agent', $session['user_agent']); } $query = $this->CI->db->get($this->sess_table_name); $counter++; } if ( !$query || $query->num_rows() == 0) { $this->CI->db->where('session_id', $session['session_id']); $query = $this->CI->db->get( $this->sess_table_name ); $this->sess_destroy(); return FALSE; } 美学:

group

来自ggplot(X, aes(days, Score)) + geom_jitter(width = 0.3, height = 0.3, aes(colour = as.factor(Study))) + geom_line(aes(group = Id), lty = 2, colour = "purple") 的说明:

  

?geom_line美学确定哪些案例连在一起。