“Geom_line lines order” 可以翻译为 “几何线条线条顺序”。

huangapple go评论54阅读模式
英文:

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()

但是我得到了这个结果:

“Geom_line lines order” 可以翻译为 “几何线条线条顺序”。

我需要这些线条按照以下顺序排列:

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 lines order” 可以翻译为 “几何线条线条顺序”。

如何告诉 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 &lt;- subset(data, Core == &quot;1&quot;, select = c(Depth, &quot;16S_B&quot;, &quot;16S_A&quot;))

And afterwards I draw the plot:

ggplot(subset_data, aes(y = Depth)) +
  geom_line(aes(x = &quot;16S_B&quot;, color = &quot;16S_B&quot;)) +
  geom_line(aes(x = &quot;16S_A&quot;, color = &quot;16S_A&quot;)) +
  labs(x = &quot;Value&quot;, y = &quot;Depth&quot;, color = &quot;Variable&quot;) +
  scale_color_manual(values = c(&quot;16S_B&quot; = &quot;blue&quot;, &quot;16S_A&quot; = &quot;red&quot;)) +
  ggtitle(&quot;Gr&#225;fico de l&#237;neas de 16S_B y 16S_A para Core 1&quot;) +
  scale_y_reverse(limits = c(31, 4)) +
  scale_x_log10()

But I get this:

“Geom_line lines order” 可以翻译为 “几何线条线条顺序”。

And I need the lines to be ordered like this:

ggplot(subset_data, aes(x = Depth)) +
  geom_line(aes(y = &quot;16S_B&quot;, color = &quot;16S_B&quot;)) +
  geom_line(aes(y = &quot;16S_A&quot;, color = &quot;16S_A&quot;)) +
  labs(y = &quot;Value&quot;, x = &quot;Depth&quot;, color = &quot;Variable&quot;) +
  scale_color_manual(values = c(&quot;16S_B&quot; = &quot;blue&quot;, &quot;16S_A&quot; = &quot;red&quot;)) +
  ggtitle(&quot;Gr&#225;fico de l&#237;neas de 16S_B y 16S_A para Core 1&quot;) +
  scale_x_reverse(limits = c(31, 4)) +
  scale_y_log10()

“Geom_line lines order” 可以翻译为 “几何线条线条顺序”。

How do I tell geom_line to link my dots in 0-15-30 order?

答案1

得分: 0

默认情况下(参见?geom_line

> geom_line() 按照 x 轴上的变量顺序连接。

要按照 y 轴上的变量顺序连接点,可以设置 orientation = &quot;y&quot;

使用一些虚拟示例数据:

subset_data &lt;- data.frame(
  check.names = FALSE,
  Depth = c(1, 15, 30),
  &quot;16S_B&quot; = c(3, 1, 2),
  &quot;16S_A&quot; = c(1, 3, 2)
)

library(ggplot2)

ggplot(subset_data, aes(y = Depth)) +
  geom_line(aes(x = `16S_B`, color = &quot;16S_B&quot;), orientation = &quot;y&quot;) +
  geom_line(aes(x = `16S_A`, color = &quot;16S_A&quot;), orientation = &quot;y&quot;) +
  labs(x = &quot;Value&quot;, y = &quot;Depth&quot;, color = &quot;Variable&quot;) +
  scale_color_manual(values = c(&quot;16S_B&quot; = &quot;blue&quot;, &quot;16S_A&quot; = &quot;red&quot;)) +
  ggtitle(&quot;Gr&#225;fico de l&#237;neas de 16S_B y 16S_A para Core 1&quot;) +
  scale_y_reverse()

“Geom_line lines order” 可以翻译为 “几何线条线条顺序”。<!-- -->

或者作为第二个选项,交换 xy 的角色,并使用 coord_flip()

ggplot(subset_data, aes(x = Depth)) +
  geom_line(aes(y = `16S_B`, color = &quot;16S_B&quot;)) +
  geom_line(aes(y = `16S_A`, color = &quot;16S_A&quot;)) +
  labs(x = &quot;Value&quot;, y = &quot;Depth&quot;, color = &quot;Variable&quot;) +
  scale_color_manual(values = c(&quot;16S_B&quot; = &quot;blue&quot;, &quot;16S_A&quot; = &quot;red&quot;)) +
  ggtitle(&quot;Gr&#225;fico de l&#237;neas de 16S_B y 16S_A para Core 1&quot;) +
  scale_x_reverse() +
  coord_flip()

“Geom_line lines order” 可以翻译为 “几何线条线条顺序”。<!-- -->

英文:

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 = &quot;y&quot;.

Using some fake example data:

subset_data &lt;- data.frame(
  check.names = FALSE,
  Depth = c(1, 15, 30),
  &quot;16S_B&quot; = c(3, 1, 2),
  &quot;16S_A&quot; = c(1, 3, 2)
)

library(ggplot2)

ggplot(subset_data, aes(y = Depth)) +
  geom_line(aes(x = `16S_B`, color = &quot;16S_B&quot;), orientation = &quot;y&quot;) +
  geom_line(aes(x = `16S_A`, color = &quot;16S_A&quot;), orientation = &quot;y&quot;) +
  labs(x = &quot;Value&quot;, y = &quot;Depth&quot;, color = &quot;Variable&quot;) +
  scale_color_manual(values = c(&quot;16S_B&quot; = &quot;blue&quot;, &quot;16S_A&quot; = &quot;red&quot;)) +
  ggtitle(&quot;Gr&#225;fico de l&#237;neas de 16S_B y 16S_A para Core 1&quot;) +
  scale_y_reverse()

“Geom_line lines order” 可以翻译为 “几何线条线条顺序”。<!-- -->

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 = &quot;16S_B&quot;)) +
  geom_line(aes(y = `16S_A`, color = &quot;16S_A&quot;)) +
  labs(x = &quot;Value&quot;, y = &quot;Depth&quot;, color = &quot;Variable&quot;) +
  scale_color_manual(values = c(&quot;16S_B&quot; = &quot;blue&quot;, &quot;16S_A&quot; = &quot;red&quot;)) +
  ggtitle(&quot;Gr&#225;fico de l&#237;neas de 16S_B y 16S_A para Core 1&quot;) +
  scale_x_reverse() +
  coord_flip()

“Geom_line lines order” 可以翻译为 “几何线条线条顺序”。<!-- -->

huangapple
  • 本文由 发表于 2023年5月11日 18:46:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定