I'm trying to make a line chart of the density of different species along a vegetation transect.
There are two transects (Transect A and Transect B), which I would like to plot separately.
"Cover" describes the %cover of each species within a zone. Start..m. and End..m. are the start and end points for each zone (some zones are longer than others).
> str(veg2)
'data.frame': 237 obs. of 8 variables:
$ Transect : chr "Transect A" "Transect A" "Transect A" "Transect A" ...
$ Zone : chr "A1" "A1" "A1" "A1" ...
$ Scientific.name : chr "Bromus diandrus" "Panicum hillmanii" "Lolium rigidum" "Rumex brownii" ...
$ Common.name..lookup.: chr "Great Brome" "Witch Panic" "Wimmera Rye-grass" "Slender Dock" ...
$ Density : chr "D" "Sp" "VS" "Sc" ...
$ Cover : int 85 20 5 1 1 1 20 1 5 5 ...
$ Start..m. : num 0 0 0 0 0 0 1.69 1.69 1.69 1.69 ...
$ End..m. : num 1.69 1.69 1.69 1.69 1.69 1.69 5.13 5.13 5.13 5.13 ...
I also have information about each species. I'm mostly interested in "Wetland Dependence" (a series of one or two letter codes) and whether or not the species is indigenous or introduced.
> str(spp)
'data.frame': 270 obs. of 4 variables:
$ Scientific.Name : chr "Aira caryophyllea subsp. caryophyllea" "Aira cupaniana" "Alternanthera denticulata s.s." "Althenia australis" ...
$ Common.Name : chr "Silvery Hair-grass" "Quicksilver Grass" "Lesser Joyweed" "Austral Water-mat" ...
$ Wetland.Dependence: chr "T" "T" "MF" "OA" ...
$ Origin_Desc : chr "Introduced" "Introduced" "Indigenous" "Indigenous" ...
First, I join the two datasets:
veg_join <- veg2 %>%
left_join(
spp %>% select(Scientific.Name, Origin_Desc, Wetland.Dependence),
by = c("Scientific.name" = "Scientific.Name")
) %>%
mutate(
Origin_Desc = ifelse(is.na(Origin_Desc), "Other", Origin_Desc),
Origin_Desc = factor(Origin_Desc,
levels = c("Indigenous", "Introduced", "Other"))
) %>%
arrange(Origin_Desc, Wetland.Dependence, Scientific.name) %>%
mutate(
Scientific.name = factor(Scientific.name,
levels = unique(Scientific.name))
)
Then I look at Transect A:
# Filter for transect A
remove_spp <- c("unknown thistle", "unidentified herb")
transectA <- veg_join %>%
filter(Transect == "Transect A") %>%
filter(!Scientific.name %in% remove_spp)
Then I plot the data. The thickness of the line segments is Cover, and they are colour-coded by "wetland dependence". The plot is split into facets for "Indigenous", "Introduced" and "NA" (for bare ground and water).
It's all looking good EXCEPT the order of the labels on the y-axis (Scientific Name) is messy. Currently, species seem to be ordered by Wetland Dependence within each Origin_Desc facet. However, I'd like it to be alphabetical in each facet (ordered from top to bottom), regardless of Wetland Dependence.
I'm so close but I need a little help tidying up this last little bit!
ggplot(transectA) +
geom_segment(
aes(x = Start..m., xend = End..m.,
y = Scientific.name, yend = Scientific.name,
linewidth = Cover, colour = Wetland.Dependence),
lineend = "round"
) +
scale_color_manual(values = fg_cols) +
scale_linewidth(range = c(1, 4)) +
ggh4x::facet_grid2(
Origin_Desc ~ .,
scales = "free_y",
space = "free_y"
) +
theme_minimal(base_size = 14)+
labs(
title = "Transect A",
x = "Distance (m)",
y = "Scientific Name",
linewidth = "Cover (%)"
)


ggplot2andaxis orderis resolved quickly withfactor(.., levels=..)or something fromforcats::fct_*().