改变图表的位置

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

Changing the position of a graph

问题

现在只有T1和T2两个变量,但我希望它继续增加,图形保持在侧边

但图形不断添加到底部

我该怎么办?

我使用了这段代码。

  1. X <- read.table(textConnection("depth T1 T2
  2. 0 3 2
  3. 1 1 0
  4. 2 2 6
  5. 3 5 7
  6. 4 10 8
  7. 5 8 10"), header=TRUE)
  8. library(reshape)
  9. mX <- melt(X, id.var="depth")
  10. names(mX)[2:3] <- c("species", "abundance")
  11. mX$fabund <- cut(mX$abundance,
  12. breaks=c(-0.01, 0, 5, 20, 100),
  13. labels=c("Abs", "Rare", "Common", "Abundant"))
  14. library(ggplot2)
  15. p <- ggplot(mX, aes(y=depth))
  16. p
  17. ## plot by proportion
  18. p +
  19. geom_ribbon(aes(xmax = -1/2*abundance, xmin = +1/2*abundance)) +
  20. facet_grid(species ~ .) +
  21. coord_fixed(ratio = 2) +
  22. theme_bw() +
  23. xlim(-10, 10) +
  24. ylim(5, 0) +
  25. xlab("Abundance (inds./㎥)") +
  26. ylab("Depth (m)")

图片链接: 点击这里

英文:

改变图表的位置

Right now, there are only T1 and T2 variables, but I hope it keeps increasing and the graph keeps sticking to the side

But the graph keeps adding to the bottom

What should I do?

I used this code.

  1. X &lt;- read.table(textConnection(&quot;depth T1 T2
  2. 0 3 2
  3. 1 1 0
  4. 2 2 6
  5. 3 5 7
  6. 4 10 8
  7. 5 8 10&quot;),header=TRUE)
  8. library(reshape)
  9. mX &lt;- melt(X,id.var=&quot;depth&quot;)
  10. names(mX)[2:3] &lt;- c(&quot;species&quot;,&quot;abundance&quot;)
  11. mX$fabund &lt;- cut(mX$abundance,
  12. breaks=c(-0.01,0,5,20,100),
  13. labels=c(&quot;Abs&quot;,&quot;Rare&quot;,&quot;Common&quot;,&quot;Abundant&quot;))
  14. library(ggplot2)
  15. p &lt;- ggplot(mX, aes(y=depth))
  16. p
  17. ## plot by proportion
  18. p +
  19. geom_ribbon(aes(xmax = -1/2*abundance, xmin = +1/2*abundance))+
  20. facet_grid(species ~ .)+
  21. coord_fixed(ratio = 2)+
  22. theme_bw()+
  23. xlim(-10,10)+
  24. ylim(5,0)+
  25. xlab(&quot;Abundance (inds./㎥)&quot;)+
  26. ylab(&quot;Depth (m)&quot;)

答案1

得分: 1

我建议使用 facet_wrap() 而不是 facet_grid(),并使用 nrow=1 来指定只想要一行的图表。

  1. ggplot(mX, aes(y=depth)) +
  2. geom_ribbon(aes(xmax = -1/2*abundance, xmin = +1/2*abundance)) +
  3. facet_wrap(~species, nrow=1) +
  4. coord_fixed(ratio = 2) +
  5. theme_bw() +
  6. xlim(-10, 10) +
  7. ylim(5, 0) +
  8. xlab("Abundance (inds./㎥)") +
  9. ylab("Depth (m)")

改变图表的位置

  1. [1]: https://i.stack.imgur.com/HaZ0a.png
  2. <details>
  3. <summary>英文:</summary>
  4. I would suggest using `facet_wrap()` instead of `facet_grid()` and specify that you only want one row of plots using `nrow=1`

ggplot(mX, aes(y=depth)) +
geom_ribbon(aes(xmax = -1/2abundance, xmin = +1/2abundance))+
facet_wrap(~species,nrow=1)+
coord_fixed(ratio = 2)+
theme_bw()+
xlim(-10,10)+
ylim(5,0)+
xlab("Abundance (inds./㎥)")+
ylab("Depth (m)")

  1. [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/HaZ0a.png
  3. </details>

huangapple
  • 本文由 发表于 2023年7月24日 19:03:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753829.html
匿名

发表评论

匿名网友

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

确定