R 如何通过线图将y轴上的所有点连接到同一组图的x轴点
我是R新手,一直在为一个朋友玩弄一些ggplot逻辑。我试图将Y轴上的所有点连接到x轴上属于同一组的点。但是我的Y轴点也被连接起来了,这是我不想要的R 如何通过线图将y轴上的所有点连接到同一组图的x轴点,r,plot,ggplot2,R,Plot,Ggplot2,我是R新手,一直在为一个朋友玩弄一些ggplot逻辑。我试图将Y轴上的所有点连接到x轴上属于同一组的点。但是我的Y轴点也被连接起来了,这是我不想要的 ggplot(data=data, aes(x= Letter_coding, y= Lectin_C, group=Island, color = Island)) + geom_line()+ geom_point() 像这样的 编辑1: 样本数据: Organism Letter_coding Island
ggplot(data=data, aes(x= Letter_coding, y= Lectin_C, group=Island,
color = Island)) +
geom_line()+
geom_point()
像这样的
编辑1:
样本数据:
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
任何帮助都将不胜感激。谢谢我将在开始时声明,此答案不是对您问题的直接回答。我认为你需要解决两个问题 第一:使用直线连接点的基本原理是什么?一般来说,随着时间的变化,我们会这样做。这在您的数据中并不明显 第二:你想在图表中传达的数据是什么?我看到一个因变量,
Lectin_C
,和两类:Island
和有机体
。我不完全理解字母编码。那有什么有趣的?凝集素C的含量是按岛屿还是按生物体,或者可能是按两者
如果我在绘制这些数据,我会将这些值绘制为抖动点,按岛屿或生物之一着色,并按生物或岛屿将数据分成组(面)。例如,如果我们主要对生物体的凝集素C
感兴趣:
library(tidyverse)
coral_data %>%
ggplot(aes(Letter_coding, Lectin_C)) +
geom_jitter(aes(color = Island)) +
facet_wrap(~Organism) + theme_bw()
或者相反,如果我们想按岛屿查看凝集素C:
您似乎希望按岛屿对数据进行分组,然后通过线条将珊瑚生物与珊瑚生物连接起来。您可以通过使用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)
#
#加载tidyverse,其中包括ggplot2、dplyr和stringr包
#
图书馆(tidyverse)
#
#数据应在island_数据中
#分离珊瑚和珊瑚生物
#
珊瑚%过滤器(str_检测(生物体,“珊瑚”))
C_破折号%过滤器(str_检测(有机体,“C-”))
#
#通过连接coral和C_数据为线段端点生成数据
#
plt_data@r2evans用样本数据更新了问题,即样本数据是一个良好的开端,但三行数据(1)没有显示足够的可变性,也(2)与样本图不匹配。此外,很难“消费”,因为我不能简单地高亮显示、复制和粘贴到R缓冲区中。请使用类似于dput(head(x,n=10))
(假设10行提供了足够的可变性)。两件事:建议的分组(文本)与显示的行(图形)不一致。除了$Lectin_C
之外,这两个点在所有列中都是相同的;如果这是点,那么其他行也不应该有连接线(例如,4,5;10,11;13,14;…)。您需要澄清您的逻辑和预期输出,因为到目前为止您提供的内容是不一致的。第二:这可能是重复的,我想你的逻辑是,你不希望直线垂直;在回来之前,他们应该总是从一组(字母)换到另一组。不幸的是,您的数据中没有任何东西表明这是相关的,也没有任何可持续的稳健逻辑来确保它总是以您想要的方式发生。一个可能相关的切题:为什么从A到B再回到A有意义,数据如何支持它?