我创建了一个函数,读取 .csv 文件,将数据处理到数据框中,在 (2, 1) 布局中制作
boxplot
和 barplot
,然后保存在 png
文件中。如果我运行一个函数,它确实会显示正确的数据框,但绘图不会保存。如果我独立运行绘图代码,Rstudio 会正确显示绘图。
data <- read.csv("file name here", stringsAsFactors = FALSE)
F4 <- function(data) {
data <- data[, !colnames(data) %in% "blank"]
names(data) <- c("district", "confirmed", "deceased", "recovered")
data[,"active"] <- data$confirmed - data$recovered - data$deceased
data <- data[, c("district", "active", "confirmed", "recovered", "deceased")]
data <- data[1:30, ]
return(data)
png("png file name here", width=100, height=500)
par(mfrow = c(2, 1), mar = c(4, 4, 4, 4), oma = c(0, 0, 0, 0))
boxplot(data$active, data$confirmed, data$recovered, data$deceased, col = "skyblue", ylab = "No. of Cases",
main = "Boxplot of COVID-19 status")
axis(1, at = 1, labels = "Active")
axis(1, at = 2, labels = "Confirmed")
axis(1, at = 3, labels = "Recovered")
axis(1, at = 4, labels = "Deceased")
barplot(data$confirmed, col = "skyblue", ylim = c(0, 200), names.arg = 1:30,
xlab = "District", ylab = "No. of Cases", main = "COVID-19 status by region")
barplot(data$recovered, col = "yellowgreen", add = TRUE)
barplot(data$deceased, col = "tomato", add = TRUE)
legend("topright", legend = c("Confirmed", "Recovered", "Deceased"), fill=c("skyblue", "yellowgreen", "tomato"), horiz=TRUE, cex = 0.9)
dev.off()
}
绘图后使用
return
。还要确保 width=
和 height=
足够大。
F4 <- function(data) {
data <- data[, !colnames(data) %in% "blank"]
names(data) <- c("district", "confirmed", "deceased", "recovered")
data[, "active"] <- data$confirmed - data$recovered - data$deceased
data <- data[, c("district", "active", "confirmed", "recovered", "deceased")]
data <- data[1:30, ]
png("foo.png", width=800, height=1000)
par(mfrow=c(2, 1), mar=c(4, 4, 4, 4), oma=c(0, 0, 0, 0))
boxplot(data$active, data$confirmed, data$recovered, data$deceased,
col="skyblue", ylab="No. of Cases",
main="Boxplot of COVID-19 status")
axis(1, at=1, labels="Active")
axis(1, at=2, labels="Confirmed")
axis(1, at=3, labels="Recovered")
axis(1, at=4, labels="Deceased")
barplot(data$confirmed, col="skyblue", ylim=c(0, 200), names.arg=1:30,
xlab="District", ylab="No. of Cases",
main="COVID-19 status by region")
barplot(data$recovered, col="yellowgreen", add=TRUE)
barplot(data$deceased, col="tomato", add=TRUE)
legend("topright", legend=c("Confirmed", "Recovered", "Deceased"),
fill=c("skyblue", "yellowgreen", "tomato"), horiz=TRUE, cex=0.9)
dev.off()
return(data)
}
> F4(data)
district active confirmed recovered deceased
1 District 1 -2 98 91 9
2 District 2 27 114 48 39
3 District 3 99 123 20 4
4 District 4 89 195 74 32
5 District 5 61 171 62 48
6 District 6 -1 98 61 38
7 District 7 81 177 61 35
8 District 8 -64 96 116 44
9 District 9 -12 73 44 41
10 District 10 -22 120 134 8
11 District 11 70 149 51 28
12 District 12 27 138 100 11
13 District 13 107 159 33 19
14 District 14 -69 69 130 8
15 District 15 96 163 25 42
16 District 16 64 160 62 34
17 District 17 20 180 132 28
18 District 18 -57 90 132 15
19 District 19 52 138 50 36
20 District 20 -64 76 113 27
21 District 21 88 158 25 45
22 District 22 -7 54 57 4
23 District 23 0 141 114 27
24 District 24 127 153 25 1
25 District 25 -68 52 103 17
26 District 26 50 107 34 23
27 District 27 -10 91 53 48
28 District 28 25 73 31 17
29 District 29 -59 92 147 4
30 District 30 87 192 60 45
数据:
> dput(data)
structure(list(district = c("District 1", "District 2", "District 3",
"District 4", "District 5", "District 6", "District 7", "District 8",
"District 9", "District 10", "District 11", "District 12", "District 13",
"District 14", "District 15", "District 16", "District 17", "District 18",
"District 19", "District 20", "District 21", "District 22", "District 23",
"District 24", "District 25", "District 26", "District 27", "District 28",
"District 29", "District 30", "District 31", "District 32", "District 33",
"District 34", "District 35", "District 36", "District 37", "District 38",
"District 39", "District 40", "District 41", "District 42", "District 43",
"District 44", "District 45", "District 46", "District 47", "District 48",
"District 49", "District 50"), confirmed = c(98L, 114L, 123L,
195L, 171L, 98L, 177L, 96L, 73L, 120L, 149L, 138L, 159L, 69L,
163L, 160L, 180L, 90L, 138L, 76L, 158L, 54L, 141L, 153L, 52L,
107L, 91L, 73L, 92L, 192L, 199L, 107L, 185L, 85L, 117L, 195L,
158L, 141L, 53L, 148L, 163L, 55L, 183L, 179L, 165L, 52L, 167L,
198L, 51L, 151L), deceased = c(9L, 39L, 4L, 32L, 48L, 38L, 35L,
44L, 41L, 8L, 28L, 11L, 19L, 8L, 42L, 34L, 28L, 15L, 36L, 27L,
45L, 4L, 27L, 1L, 17L, 23L, 48L, 17L, 4L, 45L, 39L, 39L, 20L,
35L, 35L, 38L, 41L, 17L, 26L, 12L, 18L, 31L, 15L, 28L, 16L, 43L,
8L, 20L, 42L, 47L), recovered = c(91L, 48L, 20L, 74L, 62L, 61L,
61L, 116L, 44L, 134L, 51L, 100L, 33L, 130L, 25L, 62L, 132L, 132L,
50L, 113L, 25L, 57L, 114L, 25L, 103L, 34L, 53L, 31L, 147L, 60L,
85L, 75L, 117L, 44L, 111L, 126L, 80L, 33L, 143L, 114L, 51L, 47L,
137L, 56L, 132L, 24L, 54L, 97L, 33L, 141L), blank = c(-1.66109907991481,
-0.382333726873818, -0.5126502578778, 2.7018910003448, -1.36211623118972,
0.137256218558607, -1.49362506731629, -1.4704357414368, 0.124702386197007,
-0.996639134884037, -0.0018226143047082, -0.428258881425815,
-0.613671606449495, -2.02467784541911, -1.22474795035999, 0.179516441117938,
0.567620594423535, -0.492877353553475, 6.28840653511241e-05,
1.12288964337997, 1.43985574297619, -1.09711376840582, -0.117319560250177,
1.2014984009197, -0.469729580566301, -0.0524694849389963, -0.0861072982370896,
-0.887679017906432, -0.444684004884738, -0.0294448790882381,
-0.413868849057924, 1.1133860233682, -0.480992841653982, -0.433169032600729,
0.696862576552103, -1.05636841317091, -0.0406984751512149, -1.55154482234759,
1.16716954923568, -0.273645701374081, -0.467845324672254, -1.23825232798621,
-0.00776203377732663, -0.80028217795166, -0.533492329950436,
1.28767524558459, -0.175525870242127, -1.07178238415068, 0.163206882467382,
-0.36273841562795)), class = "data.frame", row.names = c(NA,
-50L))