嗨,我是R的新手,我已经制作了一个条形图,下面是代码,但是对于数据集“Control”我没有未加工率的数据,因此有一条线条应该是在图表上(见下图)但没有数据!有没有办法让我摆脱它?我是否需要编辑txt文件中的原始数据表?
barplot(xtabs(ana$Infiltration ~ ana$Grazing + ana$Burn ),beside = TRUE, col = c( "tan4", "darkgreen"), xlab = "Burn Treatment", names = c( "Control", "Long Rotation", "Burned 1954", "Short Rotation" ) , ylab = "Mean Infiltration Rate (mm/h) " , legend = c( "Grazed", "Ungrazed"), args.legend = list(title = "Graze Treatment", x = "topright", cex = .7), ylim = c(0, 40000) )
答案 0 :(得分:1)
选项是将xtabs
中的0值替换为缺失值,即NA
tbl <- xtabs(Infiltration ~ Grazing + Burn, data = ana )
is.na(tbl) <- tbl == 0
barplot(tbl,beside = TRUE,
col = c( "tan4", "darkgreen"), xlab = "Burn Treatment",
names = c( "Control", "Long Rotation", "Burned 1954", "Short Rotation" ) ,
ylab = "Mean Infiltration Rate (mm/h) " ,
legend = c( "Grazed", "Ungrazed"),
args.legend = list(title = "Graze Treatment", x = "topright",
cex = .7), ylim = c(0, 20) )
-output
下面,我们在左侧和右侧没有NA替换的NA更换
set.seed(24)
ana <- data.frame(Infiltration = sample(1:5, 20, replace = TRUE),
Grazing = sample(c("Grazed", "Ungrazed"), 20, replace = TRUE),
Burn = sample(c("Control", "Long Rotation", "Burned 1954", "Short Rotation"),
20, replace = TRUE), stringsAsFactors = TRUE)