英文:
Geom_line lines order
问题
I'm trying to represent a soil profile where my x axis it's copies g and y is depth 0, 15, 30. But the command geom_line doesn't draw the lines in the correct order:
首先,我选择了仅来自组 Core: 1 的数据:
subset_data <- subset(data, Core == "1", select = c(Depth, "16S_B", "16S_A"))
然后,我绘制图表:
ggplot(subset_data, aes(y = Depth)) +
geom_line(aes(x = "16S_B", color = "16S_B")) +
geom_line(aes(x = "16S_A", color = "16S_A")) +
labs(x = "Value", y = "Depth", color = "Variable") +
scale_color_manual(values = c("16S_B" = "blue", "16S_A" = "red")) +
ggtitle("Gráfico de líneas de 16S_B y 16S_A para Core 1") +
scale_y_reverse(limits = c(31, 4)) +
scale_x_log10()
但是我得到了这个结果:
我需要这些线条按照以下顺序排列:
ggplot(subset_data, aes(x = Depth)) +
geom_line(aes(y = "16S_B", color = "16S_B")) +
geom_line(aes(y = "16S_A", color = "16S_A")) +
labs(y = "Value", x = "Depth", color = "Variable") +
scale_color_manual(values = c("16S_B" = "blue", "16S_A" = "red")) +
ggtitle("Gráfico de líneas de 16S_B y 16S_A para Core 1") +
scale_x_reverse(limits = c(31, 4)) +
scale_y_log10()
如何告诉 geom_line 将我的点按照 0-15-30 的顺序连接起来?
英文:
I'm trying to represent a soil profile where my x axis it's copies g and y is depth 0, 15, 30. But the command geom_line doesn't draw the lines in the correct order:
Frist I've selected the data just from the group Core: 1
subset_data <- subset(data, Core == "1", select = c(Depth, "16S_B", "16S_A"))
And afterwards I draw the plot:
ggplot(subset_data, aes(y = Depth)) +
geom_line(aes(x = "16S_B", color = "16S_B")) +
geom_line(aes(x = "16S_A", color = "16S_A")) +
labs(x = "Value", y = "Depth", color = "Variable") +
scale_color_manual(values = c("16S_B" = "blue", "16S_A" = "red")) +
ggtitle("Gráfico de líneas de 16S_B y 16S_A para Core 1") +
scale_y_reverse(limits = c(31, 4)) +
scale_x_log10()
But I get this:
And I need the lines to be ordered like this:
ggplot(subset_data, aes(x = Depth)) +
geom_line(aes(y = "16S_B", color = "16S_B")) +
geom_line(aes(y = "16S_A", color = "16S_A")) +
labs(y = "Value", x = "Depth", color = "Variable") +
scale_color_manual(values = c("16S_B" = "blue", "16S_A" = "red")) +
ggtitle("Gráfico de líneas de 16S_B y 16S_A para Core 1") +
scale_x_reverse(limits = c(31, 4)) +
scale_y_log10()
How do I tell geom_line to link my dots in 0-15-30 order?
答案1
得分: 0
默认情况下(参见?geom_line
)
> geom_line() 按照 x 轴上的变量顺序连接。
要按照 y 轴上的变量顺序连接点,可以设置 orientation = "y"
。
使用一些虚拟示例数据:
subset_data <- data.frame(
check.names = FALSE,
Depth = c(1, 15, 30),
"16S_B" = c(3, 1, 2),
"16S_A" = c(1, 3, 2)
)
library(ggplot2)
ggplot(subset_data, aes(y = Depth)) +
geom_line(aes(x = `16S_B`, color = "16S_B"), orientation = "y") +
geom_line(aes(x = `16S_A`, color = "16S_A"), orientation = "y") +
labs(x = "Value", y = "Depth", color = "Variable") +
scale_color_manual(values = c("16S_B" = "blue", "16S_A" = "red")) +
ggtitle("Gráfico de líneas de 16S_B y 16S_A para Core 1") +
scale_y_reverse()
<!-- -->
或者作为第二个选项,交换 x
和 y
的角色,并使用 coord_flip()
:
ggplot(subset_data, aes(x = Depth)) +
geom_line(aes(y = `16S_B`, color = "16S_B")) +
geom_line(aes(y = `16S_A`, color = "16S_A")) +
labs(x = "Value", y = "Depth", color = "Variable") +
scale_color_manual(values = c("16S_B" = "blue", "16S_A" = "red")) +
ggtitle("Gráfico de líneas de 16S_B y 16S_A para Core 1") +
scale_x_reverse() +
coord_flip()
<!-- -->
英文:
By default (see ?geom_line
)
> geom_line() connects in order of the variable on the x axis.
To connect the points in the order of the variable on the y axis you could set orientation = "y"
.
Using some fake example data:
subset_data <- data.frame(
check.names = FALSE,
Depth = c(1, 15, 30),
"16S_B" = c(3, 1, 2),
"16S_A" = c(1, 3, 2)
)
library(ggplot2)
ggplot(subset_data, aes(y = Depth)) +
geom_line(aes(x = `16S_B`, color = "16S_B"), orientation = "y") +
geom_line(aes(x = `16S_A`, color = "16S_A"), orientation = "y") +
labs(x = "Value", y = "Depth", color = "Variable") +
scale_color_manual(values = c("16S_B" = "blue", "16S_A" = "red")) +
ggtitle("Gráfico de líneas de 16S_B y 16S_A para Core 1") +
scale_y_reverse()
<!-- -->
Or as a second option switch the role of x
and y
and use coord_flip()
:
ggplot(subset_data, aes(x = Depth)) +
geom_line(aes(y = `16S_B`, color = "16S_B")) +
geom_line(aes(y = `16S_A`, color = "16S_A")) +
labs(x = "Value", y = "Depth", color = "Variable") +
scale_color_manual(values = c("16S_B" = "blue", "16S_A" = "red")) +
ggtitle("Gráfico de líneas de 16S_B y 16S_A para Core 1") +
scale_x_reverse() +
coord_flip()
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论