對於提出一個簡單問題,我表示抱歉,但我試圖在過去幾天內找到解決方案。下面的數據框中的Sampling.Station.Number下有8個級別。因此,我試圖產生一個並列的barplot,顯示三個蝙蝠物種每個採樣站檢測稱爲:(1)Pipestrellus pygmaeus; (2)Pipestrellus pipestrellus;和(3)夜蛾夜蛾。由一個數值變量分組的多個並排壁壘圖
任何建議如何做到這一點?我做了一些搜索,但我只能找到X軸上的因子的例子,而不是通過數值變量分組的變量,任何幫助將不勝感激!
最後,我想以產生具有相同的格式,因爲這箱線圖,其中我構造的barplot:
我創建這些箱線圖與下面這樣的代碼:
Sampling.Station.labels=c("1","2","3","4","5","6","7","8")
bat.labels<-c("Pipistrellus pygmaeus", "Pipestrellus pipestrellus", "Nyctalus noctula",
"Pipistrellus pygmaeus", "Pipestrellus pipestrellus", "Nyctalus noctula",
"Pipistrellus pygmaeus", "Pipestrellus pipestrellus", "Nyctalus noctula",
"Pipistrellus pygmaeus", "Pipestrellus pipestrellus", "Nyctalus noctula",
"Pipistrellus pygmaeus", "Pipestrellus pipestrellus", "Nyctalus noctula",
"Pipistrellus pygmaeus", "Pipestrellus pipestrellus", "Nyctalus noctula",
"Pipistrellus pygmaeus", "Pipestrellus pipestrellus", "Nyctalus noctula",
"Pipistrellus pygmaeus", "Pipestrellus pipestrellus", "Nyctalus noctula")
data_long <- gather(bats1, x, Mean.Value, Saparano.Pipestrelle:Noctule)
head(data_long)
stacked.data.1<-melt(data_long, id=c('Sampling.Station', 'x'))
head(stacked.data.1)
str(stacked.data.1)
stacked.data.1=stacked.data.1[, -3]
head(stacked.data.1)
colnames(stacked.data.1)<-c("Sampling.Station", "Bat.Species", "Light.Intensity")
head(stacked.data.1)
par(mfrow = c(1,1))
boxplots.double.1=boxplot(Lighty.Intensity~Sampling.Station + Bat.Species,
data = stacked.data.1,
at = c(1:24),
ylim = c(min(0, min(0)),
max(30, na.rm = T)),
xaxt = "n",
notch=TRUE,
col = c("red", "blue", "green"),
cex.axis=0.7,
cex.labels=0.7,
ylab="Light Intensity (Lux)",
xlab="Sampling Stations",
space=1)
axis(side = 1, at = seq(3, 24, by = 1), labels = FALSE)
text(seq(3, 24, by=3), par("usr")[3] - 0.2, labels=unique(Sampling.Station.labels), srt = 45, pos = 1, xpd = TRUE, cex=0.8)
par(oma = c(4, 1, 1, 1))
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
legend("top",
legend=c("Pipistrellus pygmaeus","Pipestrellus pipestrellus","Nyctalus noctula"),
fill=c("Blue", "Red", "Green"),
xpd = TRUE, horiz = TRUE,
inset = c(0,0),
bty = "n",
col = 1:4,
cex = 0.8,
title = "Bat Species",
lty = c(1,1))
我試過理查的建議,但我仍然遇到這個錯誤消息,任何人都可以提供幫助。事先非常感謝,如果這是可能的:
data=format
Data structure:
'data.frame': 144 obs. of 5 variables:
$ Sampling.Station : num 1 1 1 2 2 2 3 3 3 4 ...
$ Light.Intensity.S : num 26.9 25.2 39 29.8 21.8 ...
$ Number.of.bat.passes: num 3 2 5 6 15 2 10 12 17 2 ...
$ Bat.Species : Factor w/ 3 levels "Common.Pipestrelle",..: 3 3 3 3 3 3 3 3 3 3 ...
$ Simpsons.Index : num 0.4444 0 0 0.0278 0 ...
df %>%
gather(key = bat.species, value = value, -station) %>%
mutate(station = as.factor(station)) %>%
ggplot(aes(x = station, y = value, colour = variable)) +
geom_boxplot() +
facet_grid(~bat.species, scales = "free_y")
**Error in eval(expr, envir, enclos) : object 'Sampling.Station' not found**
數據幀
bats1<-structure(list(Sampling.Station = c(1, 1, 1, 2, 2, 2, 3, 3, 3,
4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 2, 2, 2, 3, 3, 3,
4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 1, 1, 1, 1, 1, 1,
2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8,
2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8,
1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6,
7, 7, 7, 8, 8, 8, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6,
7, 7, 7, 8, 8, 8, 1, 1, 1), Light.Intensity = c(26.9, 25.16,
39, 29.81, 21.83, 20.22, 2.9, 2.1, 0.85, 0.62, 0.39, 0.26, 24.7,
21.99, 20.46, 26.32, 0, 0, 0.43, 0.02, 0.02, 0.03, 0.02, 0.03,
293.56, 167.79, 114.06, 17.22, 16.26, 4.76, 0.63, 0.56, 0.56,
86.63, 87.97, 88.59, 0.31, 0.04, 0.05, 0, 0.01, 0.01, 0.02, 2.6,
2.68, 2.62, 0.43, 0.44, 26.9, 25.16, 39, 29.81, 21.83, 20.22,
2.9, 2.1, 0.85, 0.62, 0.39, 0.26, 24.7, 21.99, 20.46, 26.32,
0, 0, 0.43, 0.02, 0.02, 0.03, 0.02, 0.03, 293.56, 167.79, 114.06,
17.22, 16.26, 4.76, 0.63, 0.56, 0.56, 86.63, 87.97, 88.59, 0.31,
0.04, 0.05, 0, 0.01, 0.01, 0.02, 2.6, 2.68, 2.62, 0.43, 0.44,
26.9, 25.16, 39, 29.81, 21.83, 20.22, 2.9, 2.1, 0.85, 0.62, 0.39,
0.26, 24.7, 21.99, 20.46, 26.32, 0, 0, 0.43, 0.02, 0.02, 0.03,
0.02, 0.03, 293.56, 167.79, 114.06, 17.22, 16.26, 4.76, 0.63,
0.56, 0.56, 86.63, 87.97, 88.59, 0.31, 0.04, 0.05, 0, 0.01, 0.01,
0.02, 2.6, 2.68, 2.62, 0.43, 0.44), Number.of.bat.passes = c(3,
2, 5, 6, 15, 2, 10, 12, 17, 2, 0, 0, 15, 7, 17, 0, 1, 0, 14,
10, 12, 7, 4, 1, 3, 5, 3, 1, 6, 11, 3, 0, 0, 12, 11, 9, 1, 2,
1, 12, 14, 10, 3, 2, 1, 5, 4, 2, 3, 2, 5, 6, 15, 2, 10, 12, 17,
2, 0, 0, 15, 7, 17, 0, 1, 0, 14, 10, 12, 7, 4, 1, 3, 5, 3, 1,
6, 11, 3, 0, 0, 12, 11, 9, 1, 2, 1, 12, 14, 10, 3, 2, 1, 5, 4,
2, 3, 2, 5, 6, 15, 2, 10, 12, 17, 2, 0, 0, 15, 7, 17, 0, 1, 0,
14, 10, 12, 7, 4, 1, 3, 5, 3, 1, 6, 11, 3, 0, 0, 12, 11, 9, 1,
2, 1, 12, 14, 10, 3, 2, 1, 5, 4, 2), Bat.Species = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Common.Pipestrelle",
"Noctule", "Saprano.Pipestrelle"), class = "factor"), Simpsons.Index = c(0.444444444,
0, 0, 0.027777778, 0, 0, 0.25, 0, 0.08650519, 0, 0, 0, 0.111111111,
0, 0.124567474, 0, 0, 0, 0.25, 0.01, 0.111111111, 0.081632653,
0, 0, 0, 0.04, 0, 1, 0.027777778, 0.033057851, 0.111111111, 0,
0, 0.027777778, 0.074380165, 0., 0, 0, 1, 0.173611111,
0.081632653, 0.16, 1, 0.25, 0, 0.04, 0.25, 0.25, 0.25, 0, 0,
7, 0, 0, 0, 0, 0.08, 0, 0, 0, 0.6, 0, 0.222222222, 0, 0, 0, 0.142857143,
9, 0.5, 1.25, 0, 0, 0, 4, 0, 0, 5, 2, 1, 0, 0, 1.25, 0.888888889,
5, 0, 0, 2, 0.28, 0.625, 0.375, 0, 1, 0, 4, 0.5, 1, 0, 0, 1,
0, 0.109375, 0, 0, 0, 0.75, 0, 0, 0, 0, 0.08, 0.046875, 0, 0,
0, 0, 0, 0.046875, 0, 0, 0, 0, 0.0625, 0, 0, 0, 0.015625, 0,
0, 0, 0.28, 0, 0.12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA,
-144L), .Names = c("Sampling.Station", "Light.Intensity", "Number.of.bat.passes",
"Bat.Species", "Simpsons.Index"), class = "data.frame")
你想在y軸上的光譜值?你想用D.SP,D.CP和D.N列的值來做什麼?你不是說你想要那些在Y軸上?我很困惑。 – MrFlick
圖像顯示箱型圖,而不是條形圖。你想要什麼? – AlexR
使用'tidyr :: gather'來重塑你的數據框,然後facet_wrap變量名。 –