我正在创建一个ggplot分组条形图,其中包含学校作业的错误栏。我对ggplot很新,但是作业规定我必须使用ggplot作为这个情节。但是我不断收到错误说"错误:美学必须是长度1或与数据(6)相同:x,y,fill"。我该如何解决?为我的数据混乱提前道歉。
drought = read.csv("drought_elevation.csv")
drought
#Packages
> drought = read.csv("drought_elevation.csv")
> drought
spec elevation treat height
1 A. australis low ctrl 24.9
2 A. australis low ctrl 25.3
3 A. australis low ctrl 25.7
4 A. australis low ctrl 33.0
5 A. australis low ctrl 31.2
6 A. australis low ctrl 25.5
7 A. australis low ctrl 20.8
8 A. australis low ctrl 28.6
9 A. australis low ctrl 26.8
10 A. australis low ctrl 24.5
11 P. totara low ctrl 34.5
12 P. totara low ctrl 29.2
13 P. totara low ctrl 27.7
14 P. totara low ctrl 25.7
15 P. totara low ctrl 34.1
16 P. totara low ctrl 28.9
17 P. totara low ctrl 34.1
18 P. totara low ctrl 22.8
19 P. totara low ctrl 39.9
20 P. totara low ctrl 33.9
21 B. tawa low ctrl 44.7
22 B. tawa low ctrl 42.5
23 B. tawa low ctrl 37.9
24 B. tawa low ctrl 41.6
25 B. tawa low ctrl 39.5
26 B. tawa low ctrl 41.3
27 B. tawa low ctrl 37.5
28 B. tawa low ctrl 34.2
29 B. tawa low ctrl 39.7
30 B. tawa low ctrl 43.4
31 A. australis mid ctrl 26.3
32 A. australis mid ctrl 16.0
33 A. australis mid ctrl 25.8
34 A. australis mid ctrl 15.7
35 A. australis mid ctrl 26.3
36 A. australis mid ctrl 25.7
37 A. australis mid ctrl 17.9
38 A. australis mid ctrl 15.4
39 A. australis mid ctrl 22.3
40 A. australis mid ctrl 29.6
41 P. totara mid ctrl 22.4
42 P. totara mid ctrl 22.0
43 P. totara mid ctrl 31.3
44 P. totara mid ctrl 27.0
45 P. totara mid ctrl 29.6
46 P. totara mid ctrl 27.2
47 P. totara mid ctrl 20.0
48 P. totara mid ctrl 30.5
49 P. totara mid ctrl 24.7
50 P. totara mid ctrl 20.8
51 B. tawa mid ctrl 27.8
52 B. tawa mid ctrl 32.6
53 B. tawa mid ctrl 29.1
54 B. tawa mid ctrl 26.5
55 B. tawa mid ctrl 31.2
56 B. tawa mid ctrl 29.3
57 B. tawa mid ctrl 25.4
58 B. tawa mid ctrl 26.5
59 B. tawa mid ctrl 32.4
60 B. tawa mid ctrl 33.5
61 A. australis high ctrl 19.1
62 A. australis high ctrl 21.2
63 A. australis high ctrl 15.0
64 A. australis high ctrl 10.9
65 A. australis high ctrl 15.5
66 A. australis high ctrl 10.0
67 A. australis high ctrl 6.4
68 A. australis high ctrl 18.4
69 A. australis high ctrl 12.6
70 A. australis high ctrl 16.6
71 P. totara high ctrl 23.3
72 P. totara high ctrl 19.1
73 P. totara high ctrl 14.6
74 P. totara high ctrl 20.0
75 P. totara high ctrl 21.6
76 P. totara high ctrl 23.5
77 P. totara high ctrl 14.7
78 P. totara high ctrl 23.9
79 P. totara high ctrl 21.7
80 P. totara high ctrl 19.6
81 B. tawa high ctrl 25.6
82 B. tawa high ctrl 16.9
83 B. tawa high ctrl 34.8
84 B. tawa high ctrl 32.4
85 B. tawa high ctrl 20.3
86 B. tawa high ctrl 17.4
87 B. tawa high ctrl 34.4
88 B. tawa high ctrl 25.5
89 B. tawa high ctrl 25.0
90 B. tawa high ctrl 25.9
91 A. australis low drought 21.1
92 A. australis low drought 14.9
93 A. australis low drought 9.3
94 A. australis low drought 18.9
95 A. australis low drought 22.9
96 A. australis low drought 21.2
97 A. australis low drought 14.4
98 A. australis low drought 24.1
99 A. australis low drought 19.0
100 A. australis low drought 8.8
101 P. totara low drought 18.2
102 P. totara low drought 24.1
103 P. totara low drought 20.2
104 P. totara low drought 17.9
105 P. totara low drought 27.6
106 P. totara low drought 15.9
107 P. totara low drought 27.3
108 P. totara low drought 22.1
109 P. totara low drought 26.6
110 P. totara low drought 29.2
111 B. tawa low drought 32.8
112 B. tawa low drought 25.9
113 B. tawa low drought 28.7
114 B. tawa low drought 34.3
115 B. tawa low drought 42.5
116 B. tawa low drought 33.0
117 B. tawa low drought 27.2
118 B. tawa low drought 19.9
119 B. tawa low drought 25.8
120 B. tawa low drought 31.1
121 A. australis mid drought 18.7
122 A. australis mid drought 14.9
123 A. australis mid drought 14.9
124 A. australis mid drought 16.2
125 A. australis mid drought 7.7
126 A. australis mid drought 11.0
127 A. australis mid drought 8.2
128 A. australis mid drought 25.9
129 A. australis mid drought 21.3
130 A. australis mid drought 1.7
131 P. totara mid drought 12.1
132 P. totara mid drought 19.9
133 P. totara mid drought 12.8
134 P. totara mid drought 28.1
135 P. totara mid drought 19.6
136 P. totara mid drought 19.2
137 P. totara mid drought 15.7
138 P. totara mid drought 25.3
139 P. totara mid drought 12.5
140 P. totara mid drought 23.6
141 B. tawa mid drought 20.4
142 B. tawa mid drought 24.5
143 B. tawa mid drought 22.3
144 B. tawa mid drought 24.6
145 B. tawa mid drought 22.0
146 B. tawa mid drought 18.9
147 B. tawa mid drought 17.9
148 B. tawa mid drought 23.0
149 B. tawa mid drought 24.8
150 B. tawa mid drought 23.7
151 A. australis high drought 3.4
152 A. australis high drought 11.9
153 A. australis high drought 7.9
154 A. australis high drought 5.3
155 A. australis high drought 6.1
156 A. australis high drought 7.1
157 A. australis high drought 7.4
158 A. australis high drought 8.2
159 A. australis high drought 4.3
160 A. australis high drought 0.6
161 P. totara high drought 14.6
162 P. totara high drought 14.4
163 P. totara high drought 9.5
164 P. totara high drought 9.5
165 P. totara high drought 19.5
166 P. totara high drought 4.3
167 P. totara high drought 9.2
168 P. totara high drought 13.5
169 P. totara high drought 14.3
170 P. totara high drought 20.6
171 B. tawa high drought 19.2
172 B. tawa high drought 19.8
173 B. tawa high drought 15.5
174 B. tawa high drought 18.4
175 B. tawa high drought 16.1
176 B. tawa high drought 18.9
177 B. tawa high drought 23.1
178 B. tawa high drought 20.0
179 B. tawa high drought 8.7
180 B. tawa high drought 25.4
> #Packages
> library(ggplot2)
> library(plyr)
> library(reshape2)
> library(dplyr)
>
> #Elevations
> lowh = drought[drought$elevation %in% c("low"), ]
> midh = drought[drought$elevation %in% c("mid"), ]
> highh = drought[drought$elevation %in% c("high"), ]
>
>
> #Se function
> se <- function(x, na.rm = FALSE) {
+ if (na.rm) {
+ sqrt(var(x, na.rm = T)/length(na.omit(x)))
+ } else {
+ if (any(is.na(sqrt(var(x)/length(x))))) stop("There are NAs in the data!")
+ sqrt(var(x)/length(x))
+ }
+ }
> dat1 <- group_by(.data = lowh, treat, spec) %>%
+ summarise(height_aver = mean(height, na.rm = T),
+ se.pos = mean(height, na.rm = T) + se(height, na.rm = T),
+ se.neg = mean(height, na.rm = T) - se(height, na.rm = T))
> dat1 <- dat1[order(dat1$spec), ]
> dat1
# A tibble: 6 x 5
# Groups: treat [2]
treat spec height_aver se.pos se.neg
<fctr> <fctr> <dbl> <dbl> <dbl>
1 ctrl A. australis 26.63 27.73956 25.52044
2 drought A. australis 17.46 19.16901 15.75099
3 ctrl B. tawa 40.23 41.22029 39.23971
4 drought B. tawa 30.12 32.05975 28.18025
5 ctrl P. totara 31.08 32.68864 29.47136
6 drought P. totara 22.91 24.40179 21.41821
>
> wide <- acast(dat1, treat ~ spec, value.var = "height_aver")
>
> ####Actual plot####
> low <- ggplot(wide, aes(x = spec, y = height, fill = treat))
Error: ggplot2 doesn't know how to deal with data of class matrix
>
> cleanup = theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(color = "black"))
>
> p1= (low + cleanup + coord_cartesian(ylim = c(0, 35))
+ + geom_bar(stat = "identity", position = "dodge")
+ + theme(axis.text.x = element_text(angle = 45, hjust = 1))
+ + xlab("Species")
+ + ylab("Seedling height (cm)")
+ + scale_fill_manual(values = c("#009933", "#FFcc66"), name = "Treatment")
+ + geom_errorbar(aes(ymin=lowh$height-se, ymax=lowh$height+se), position = "dodge"))
> p1
Error: Aesthetics must be either length 1 or the same as the data (6): x, y, fill
> dev.off()