我有以下数据集和代码,并且还显示了dput()的输出。我想在y轴上绘制日志(N2012 $ Rate)列,在x轴上绘制位置,并按月垂直分割,然后按气体分割。图表在视觉上有效,但没有为因子分配正确的数据点。我不确定我做错了什么。对不起笨拙的代码,我是R的新手并自学了。
我还希望N2O面板为黄色,N2面板为蓝色。那也没有发生,我不知道为什么。
数据
Location.Month Gas Rate Month Location log(N2012$Rate)
1 HighBank July N2 24.00 July HighBank 3.1780538
2 HighBank July N2 309.00 July HighBank 5.7333413
3 HighBank July N2O 92.00 July HighBank 4.5217886
4 HighBank July N2O 45.00 July HighBank 3.8066625
5 LowBank July N2 582.00 July LowBank 6.3664704
6 LowBank July N2 27.00 July LowBank 3.2958369
7 LowBank July N2O 299.00 July LowBank 5.7004436
8 LowBank July N2O 99.00 July LowBank 4.5951199
9 Basin July N2 219.00 July Basin 5.3890717
10 Basin July N2 1096.00 July Basin 6.9994225
11 Basin July N2 151.00 July Basin 5.0172798
12 Basin July N2 326.00 July Basin 5.7868974
13 Basin July N2 135.00 July Basin 4.9052748
14 Basin July N2 29.00 July Basin 3.3672958
15 Basin July N2 140.00 July Basin 4.9416424
16 Basin July N2O 124.00 July Basin 4.8202816
17 Basin July N2O 97.00 July Basin 4.5747110
18 Basin July N2O 177.00 July Basin 5.1761497
19 Basin July N2O 702.00 July Basin 6.5539334
20 Basin July N2O 202.00 July Basin 5.3082677
21 Basin July N2O 76.00 July Basin 4.3307333
22 Basin July N2O 51.00 July Basin 3.9318256
23 HighBank Aug N2 2328.00 Aug HighBank 7.7527648
24 HighBank Aug N2 584.00 Aug HighBank 6.3699010
25 HighBank Aug N2O 1137.00 Aug HighBank 7.0361485
26 HighBank Aug N2O 1438.00 Aug HighBank 7.2710085
27 LowBank Aug N2 217.00 Aug LowBank 5.3798974
28 LowBank Aug N2 947.00 Aug LowBank 6.8532991
29 LowBank Aug N2O 398.00 Aug LowBank 5.9864520
30 LowBank Aug N2O 984.00 Aug LowBank 6.8916259
31 Basin Aug N2 787.00 Aug Basin 6.6682282
32 Basin Aug N2 365.00 Aug Basin 5.8998974
33 Basin Aug N2 597.00 Aug Basin 6.3919171
34 Basin Aug N2 6125.00 Aug Basin 8.7201340
35 Basin Aug N2 13439.00 Aug Basin 9.5059162
36 Basin Aug N2 9348.00 Aug Basin 9.1429177
37 Basin Aug N2 8679.00 Aug Basin 9.0686616
38 Basin Aug N2O 1177.00 Aug Basin 7.0707241
39 Basin Aug N2O 119.00 Aug Basin 4.7791235
40 Basin Aug N2O 182.00 Aug Basin 5.2040067
41 Basin Aug N2O 352.00 Aug Basin 5.8636312
42 Basin Aug N2O 4051.00 Aug Basin 8.3067190
43 Basin Aug N2O 1921.00 Aug Basin 7.5606012
44 Basin Aug N2O 2017.00 Aug Basin 7.6093665
45 HighBank Sept N2 25.70 Sept HighBank 3.2464910
46 HighBank Sept N2 28.30 Sept HighBank 3.3428618
47 HighBank Sept N2O 17.70 Sept HighBank 2.8735646
48 HighBank Sept N2O 157.00 Sept HighBank 5.0562458
49 LowBank Sept N2 6.19 Sept LowBank 1.8229351
50 LowBank Sept N2 67.60 Sept LowBank 4.2136080
51 LowBank Sept N2O 9.51 Sept LowBank 2.2523439
52 LowBank Sept N2O 244.00 Sept LowBank 5.4971682
53 Basin Sept N2 1077.00 Sept Basin 6.9819347
54 Basin Sept N2 160.00 Sept Basin 5.0751738
55 Basin Sept N2 949.00 Sept Basin 6.8554088
56 Basin Sept N2 714.00 Sept Basin 6.5708830
57 Basin Sept N2 28.30 Sept Basin 3.3428618
58 Basin Sept N2 1838.00 Sept Basin 7.5164333
59 Basin Sept N2 249.00 Sept Basin 5.5174529
60 Basin Sept N2O 397.00 Sept Basin 5.9839363
61 Basin Sept N2O 197.00 Sept Basin 5.2832037
62 Basin Sept N2O 366.00 Sept Basin 5.9026333
63 Basin Sept N2O 27.60 Sept Basin 3.3178158
64 Basin Sept N2O 44.20 Sept Basin 3.7887248
65 Basin Sept N2O 112.00 Sept Basin 4.7184989
66 Basin Sept N2O 171.00 Sept Basin 5.1416636
67 HighBank Oct N2 9.43 Oct HighBank 2.2438961
68 HighBank Oct N2 3.77 Oct HighBank 1.3270750
69 HighBank Oct N2O 3.13 Oct HighBank 1.1410330
70 HighBank Oct N2O 19.10 Oct HighBank 2.9496883
71 LowBank Oct N2 2.39 Oct LowBank 0.8712934
72 LowBank Oct N2 5.66 Oct LowBank 1.7334239
73 LowBank Oct N2O 40.20 Oct LowBank 3.6938670
74 LowBank Oct N2O 27.80 Oct LowBank 3.3250360
75 Basin Oct N2 55.60 Oct Basin 4.0181832
76 Basin Oct N2 20.20 Oct Basin 3.0056826
77 Basin Oct N2 55.20 Oct Basin 4.0109630
78 Basin Oct N2 8.90 Oct Basin 2.1860513
79 Basin Oct N2 3.72 Oct Basin 1.3137237
80 Basin Oct N2 9.06 Oct Basin 2.2038691
81 Basin Oct N2 8.04 Oct Basin 2.0844291
82 Basin Oct N2O 25.50 Oct Basin 3.2386785
83 Basin Oct N2O 29.30 Oct Basin 3.3775875
84 Basin Oct N2O 16.90 Oct Basin 2.8273136
85 Basin Oct N2O 5.17 Oct Basin 1.6428727
86 Basin Oct N2O 6.33 Oct Basin 1.8453002
87 Basin Oct N2O 7.72 Oct Basin 2.0438144
88 Basin Oct N2O 7.58 Oct Basin 2.0255132
数据结构
Data <-
structure(list(Location.Month = structure(c(6L, 6L, 6L, 6L, 10L,
10L, 10L, 10L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 5L, 5L, 5L, 5L, 9L, 9L, 9L, 9L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 8L, 8L, 8L, 8L, 12L, 12L, 12L,
12L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
7L, 7L, 7L, 7L, 11L, 11L, 11L, 11L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Basin Aug", "Basin July",
"Basin Oct", "Basin Sept", "HighBank Aug", "HighBank July", "HighBank Oct",
"HighBank Sept", "LowBank Aug", "LowBank July", "LowBank Oct",
"LowBank Sept"), class = "factor"), Gas = structure(c(1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("N2", "N2O"), class = "factor"),
Rate = c(24, 309, 92, 45, 582, 27, 299, 99, 219, 1096, 151,
326, 135, 29, 140, 124, 97, 177, 702, 202, 76, 51, 2328,
584, 1137, 1438, 217, 947, 398, 984, 787, 365, 597, 6125,
13439, 9348, 8679, 1177, 119, 182, 352, 4051, 1921, 2017,
25.7, 28.3, 17.7, 157, 6.19, 67.6, 9.51, 244, 1077, 160,
949, 714, 28.3, 1838, 249, 397, 197, 366, 27.6, 44.2, 112,
171, 9.43, 3.77, 3.13, 19.1, 2.39, 5.66, 40.2, 27.8, 55.6,
20.2, 55.2, 8.9, 3.72, 9.06, 8.04, 25.5, 29.3, 16.9, 5.17,
6.33, 7.72, 7.58), Month = structure(c(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, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("July", "Aug",
"Sept", "Oct"), class = "factor"), Location = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("HighBank",
"LowBank", "Basin"), class = "factor"), `log(N2012$Rate)` = c(3.17805383034795,
5.73334127689775, 4.52178857704904, 3.80666248977032, 6.36647044773144,
3.29583686600433, 5.70044357339069, 4.59511985013459, 5.3890717298165,
6.99942246750796, 5.01727983681492, 5.78689738136671, 4.90527477843843,
3.36729582998647, 4.9416424226093, 4.82028156560504, 4.57471097850338,
5.17614973257383, 6.55393340402581, 5.30826769740121, 4.33073334028633,
3.93182563272433, 7.75276480885133, 6.36990098282823, 7.03614849375054,
7.27100853828099, 5.37989735354046, 6.85329909318608, 5.98645200528444,
6.89162589705225, 6.6682282484174, 5.89989735358249, 6.3919171133926,
8.72013403541293, 9.50591620654706, 9.14291769565875, 9.06866159364425,
7.07072410726028, 4.77912349311153, 5.2040066870768, 5.8636311755981,
8.30671904320269, 7.56060116276856, 7.60936653795421, 3.24649099190117,
3.34286180464919, 2.87356463957978, 5.05624580534831, 1.8229350866965,
4.21360798304892, 2.2523438765573, 5.4971682252932, 6.98193467715639,
5.07517381523383, 6.85540879860993, 6.57088296233958, 3.34286180464919,
7.51643330291563, 5.51745289646471, 5.98393628068719, 5.28320372873799,
5.90263333340137, 3.3178157727231, 3.78872478908365, 4.71849887129509,
5.14166355650266, 2.24389609664537, 1.32707500145992, 1.14103300455206,
2.94968833505258, 0.871293365943419, 1.73342389221509, 3.69386699562498,
3.32503602069659, 4.01818320125654, 3.00568260440716, 4.01096295328305,
2.18605127673809, 1.31372366828506, 2.20386912005489, 2.08442908319087,
3.23867845216438, 3.37758751602302, 2.82731362192903, 1.64287268852034,
1.84530023615608, 2.04381436403668, 2.02551319965428)), .Names = c("Location.Month",
"Gas", "Rate", "Month", "Location", "log(N2012$Rate)"), row.names = c(NA,
-88L), class = "data.frame")
代码
Data$Location = factor(Data$Location,c("HighBank", "LowBank", "Basin"))
Data$Gas = factor(Data$Gas,c("N2", "N2O"))
colvec<-c("blue", "yellow")
Data$Month = factor(Data$Month,c("July", "Aug", "Sept", "Oct"))
p <- ggplot(Data, aes(factor(Location),log(N2012$Rate), factor(Gas)))
pl <- p + geom_boxplot(fill="blue", width = 0.7) + geom_point()
plo <- pl + scale_fill_manual(values=colvec) + facet_grid(Gas ~ Month)
plot <- plo + xlab("Location") + ylab("log(N2O ug N2O-N/m2/h)")
plots <-plot + theme(axis.title.x = element_text(face="bold", colour="Black", size=15),
axis.text.x = element_text(angle=50, vjust=0.5, size=12, colour="Black"),
axis.title.y = element_text(face="bold", size=13),
axis.text.y = element_text(size=12, colour="black"))
plots
谢谢,
-Sarah
答案 0 :(得分:11)
鉴于您的数据集,您不再需要因子转换(因为已发布的示例数据已完成)。
我需要修改一些东西。首先,log(N2012$Rate)
是非标准名称,因此必须在代码中用反引号括起来:`log(N2012$Rate)`
。此外,要获得填充箱线图的不同颜色,您需要将Gas
分配给fill
美学,而不是在箱线图中覆盖它。
ggplot(Data, aes(Location, `log(N2012$Rate)`, fill=Gas)) +
geom_boxplot(width = 0.7) +
geom_point() +
facet_grid(Gas ~ Month) +
scale_fill_manual(values=colvec) +
xlab("Location") +
ylab("log(N2O ug N2O-N/m2/h)") +
theme(axis.title.x = element_text(face="bold", colour="Black", size=15),
axis.text.x = element_text(angle=50, vjust=0.5, size=12, colour="Black"),
axis.title.y = element_text(face="bold", size=13),
axis.text.y = element_text(size=12, colour="black"))