简体   繁体   English

如何通过折线图将y轴上的所有点连接到同一组ggplot的x轴点

[英]How to connect all points on a y-axis to x-axis points of same group ggplot through a line graph

I'm new to R and been playing around with some ggplot logic for a friend. 我是R的新手,并且一直在为朋友玩一些ggplot逻辑。 Where I'm trying to connect all points on a Y-axis to the points on the x-axis that belong to the same group. 我试图将Y轴上的所有点连接到属于同一组的x轴上的点的位置。 But my Y-axis points are also being connected which I don't want 但是我的Y轴点也正在连接,这是我不想要的

ggplot(data=data, aes(x= Letter_coding, y= Lectin_C, group=Island, 
color = Island)) +
geom_line()+
geom_point() 

Something like this. 这样的事情。

在此处输入图片说明

但我正在努力实现这一目标

Edit 1: 编辑1:

Sample data: 样本数据:

Organism            Letter_coding   Island          Lectin_C    
Coral (Pocillopora)     A           FlintMos3_2     77.42858683 
Coral (Pocillopora)     A           FlintMos3_2     206.5272288 
C-A (Red Algae)         B           FlintMos3_2     201.8928979 
Coral (Porites)         C           FlintMos3_5     100.0270507 
Coral (Porites)         C           FlintMos3_5     116.1427727 
C-A (Red Algae)         D           FlintMos3_5     113.2093909 
Coral (Porites)         E           FlintMos5_2     148.1921679 
C-C                     F           FlintMos5_2     140.8645009 
Coral (Porites)         E           FlintMos5_2     120.3082097 
Coral (Porites)         G           MaldInv         259.2967552 
Coral (Porites)         G           MaldInv         238.4524644 
C-A (CCA)               H           MaldInv         58.82896626 
Coral (Porites)         C           StarTent        137.056068  
Coral (Porites)         C           StarTent        107.1444611 
C-A (Red Algae)         D           StarTent        120.4673744 
Coral (Porites)         G           VostMos_2       162.9043976 
Coral (Porites)         G           VostMos_2       202.3885923 
C-A (CCA)               H           VostMos_2       144.3439106
Coral (Porites)         I           VostMos_4       309.4388754 
Coral (Porites)         I           VostMos_4       276.9731826 
C-C                     J           VostMos_4       170.3126185
Coral (Montipora)       I           VostMos_4       181.4586178 
Coral (Montipora)       I           VostMos_4       158.7184731 

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

It seems that you want to group data by island and then connect the Coral organisms to the C_ organisms by lines. 看来您想按岛屿对数据进行分组,然后按线将Coral生物与C_生物连接起来。 You can do this in a straightforward way by using several functions from tidyverse as shown below. 您可以通过使用tidyverse的几个功能以一种简单的方式完成此操作,如下所示。

#
# Load tidyverse which includes the ggplot2, dplyr, and stringr packages  
#
   library(tidyverse)
#
#  Data should be in island_data
#  Separate Coral and C_ organisms
#
   coral <- island_data %>% filter(str_detect(Organism, "Coral"))
   C_dash <- island_data %>% filter(str_detect(Organism, "C-"))
#
#   Make data for line segment ends by joining coral and C_ data
#
   plt_data <- left_join(coral, C_dash, by = "Island", suffix = c("","_C") ) 
   sp <-  plt_data %>% ggplot(aes( color = Island)) +  
          geom_segment(aes( x= Letter_coding, y = Lectin_C, xend = Letter_coding_C, yend = Lectin_C_C), size=1.1) +
           geom_point(aes(x= Letter_coding, y=Lectin_C), size = 4) +
           geom_point(aes(x = Letter_coding_C, y = Lectin_C_C), size = 4)

   plot(sp)

This gives the plot 这给出了情节

在此处输入图片说明

I will state at the outset that this answer is not a direct answer to your question. 首先,我会说这个答案不是您问题的直接答案。 I think you need to address two issues. 我认为您需要解决两个问题。

First: what is the rationale for connecting the points using lines? 第一:用线连接点的原理是什么? Generally, we do this when there is change over time. 通常,随着时间的变化我们会这样做。 That is not apparent in your data. 这在您的数据中并不明显。

Second: what is it about the data that you want to convey in the chart? 第二:关于您要在图表中传达的数据是什么? I see a dependent variable, Lectin_C , and two categories: Island and Organism . 我看到一个因变量Lectin_C和两个类别: IslandOrganism The letter coding I do not fully understand. 字母编码我不太了解。 So what's interesting? 那么有趣的是什么? Is it Lectin_C content by island, or by organism, or perhaps by both? 是按岛屿,按生物还是按两种都Lectin_C含量?

If I were plotting this data, I'd plot the values as jittered points, colour by one of island or organism and split the data into groups (facets) by organism or island. 如果要绘制此数据,则将这些值绘制为抖动点,按岛或生物体之一进行着色,然后按生物体或岛体将数据划分为组(构面)。 As an example, if we are primarily interested in Lectin_C by Organism : 例如,如果我们主要对Organism Lectin_C感兴趣:

library(tidyverse)
coral_data %>% 
ggplot(aes(Letter_coding, Lectin_C)) + 
  geom_jitter(aes(color = Island)) + 
  facet_wrap(~Organism) + theme_bw()

在此处输入图片说明

Or conversely, if we want to look at Lectin_C by Island: 或者相反,如果我们想按岛查看Lectin_C

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM