英文:
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 <- 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)")
答案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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论