改变图表的位置

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

Changing the position of a graph

问题

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

但图形不断添加到底部

我该怎么办?

我使用了这段代码。

X <- read.table(textConnection("depth T1 T2
0 3 2
1 1 0
2 2 6
3 5 7
4 10 8
5 8 10"), header=TRUE)

library(reshape)
mX <- melt(X, id.var="depth")
names(mX)[2:3] <- c("species", "abundance")
mX$fabund <- cut(mX$abundance,
                 breaks=c(-0.01, 0, 5, 20, 100),
                 labels=c("Abs", "Rare", "Common", "Abundant"))

library(ggplot2)
p <- ggplot(mX, aes(y=depth))
p

## plot by proportion
p +
  geom_ribbon(aes(xmax = -1/2*abundance, xmin = +1/2*abundance)) +
  facet_grid(species ~ .) +
  coord_fixed(ratio = 2) +
  theme_bw() +
  xlim(-10, 10) +
  ylim(5, 0) +
  xlab("Abundance (inds./㎥)") +
  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.

X &lt;- read.table(textConnection(&quot;depth T1 T2
0 3 2
1 1 0
2 2 6
3 5 7
4 10 8
5 8 10&quot;),header=TRUE)


library(reshape)
mX &lt;- melt(X,id.var=&quot;depth&quot;)
names(mX)[2:3] &lt;- c(&quot;species&quot;,&quot;abundance&quot;)
mX$fabund &lt;- cut(mX$abundance,
                 breaks=c(-0.01,0,5,20,100),
                 labels=c(&quot;Abs&quot;,&quot;Rare&quot;,&quot;Common&quot;,&quot;Abundant&quot;))

library(ggplot2)
p &lt;- ggplot(mX, aes(y=depth))
p

## plot by proportion
p +
  geom_ribbon(aes(xmax = -1/2*abundance, xmin = +1/2*abundance))+
  facet_grid(species ~ .)+
  coord_fixed(ratio = 2)+
  theme_bw()+
  xlim(-10,10)+
  ylim(5,0)+
  xlab(&quot;Abundance (inds./㎥)&quot;)+
  ylab(&quot;Depth (m)&quot;)

答案1

得分: 1

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

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

改变图表的位置


[1]: https://i.stack.imgur.com/HaZ0a.png

<details>
<summary>英文:</summary>

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


[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/HaZ0a.png

</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:

确定