I have a dataset in R that contains species at certain depths and I am trying to plot them but I don't seem to be able to make the line follow the depth instead, it wants to follow the x-axis numbers (figures will explain it better)
Dummy data: | depth | value | |:---- |:------:| | 5 | 200 | | 15 | 100 | | 25 | 50 | | 35 | 100 | | 45 | 80 | | 55 | 40 | | 65 | 10 |
I am trying to plot it like this
ggplot(test, aes(x = value, y = depth)) +
geom_line() +
geom_point() +
scale_y_reverse() +
labs(title = "Sea Depth vs Value",
x = "Value", y = "Sea Depth (cm)") +
theme_minimal()
With that code, I get this figure
but I would like to get something like this
Any help is much appreciated
You can use a geom_path
instead of a geom_line
. Whereas the latter will connect the points according to the variable mapped on x
a geom_path
will connect the points as they appear in the data:
# Dataframe erstellen
test <- data.frame(
depth = c(5, 15, 25, 35, 45, 55, 65),
value = c(200, 100, 50, 100, 80, 40, 10)
)
library(ggplot2)
ggplot(test, aes(x = value, y = depth)) +
geom_path() +
geom_point() +
scale_y_reverse() +
labs(
title = "Sea Depth vs Value",
x = "Value", y = "Sea Depth (cm)"
) +
theme_minimal()
A second option would be to use orientation="y"
for geom_line
to order by the y
values:
ggplot(test, aes(x = value, y = depth)) +
geom_line(orientation = "y") +
geom_point() +
scale_y_reverse() +
labs(
title = "Sea Depth vs Value",
x = "Value", y = "Sea Depth (cm)"
) +
theme_minimal()