Reputation: 37
I'm new to R and been playing around with some ggplot logic for a friend. 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. But my Y-axis points are also being connected which I don't want
ggplot(data=data, aes(x= Letter_coding, y= Lectin_C, group=Island,
color = Island)) +
geom_line()+
geom_point()
Something like this.
Edit 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
Upvotes: 1
Views: 2386
Reputation: 5520
It seems that you want to group data by island and then connect the Coral organisms to the C_ organisms by lines. You can do this in a straightforward way by using several functions from tidyverse as shown below.
#
# 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
Upvotes: 2
Reputation: 33772
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
. 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?
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
:
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:
Upvotes: 0