代码未在 ggplot 的 geom_bar() 中绘制因子的 0 级
Code isn't plotting level 0 of a factor in ggplot's geom_bar()
我的最终情节没有显示因子 A 的 0 水平。
Header数据:
month A B
mayo 20 6.9
mayo 16 16.3
mayo 14 19.8
mayo 12 12.6
mayo 9 19.5
mayo 8 65.4
mayo 18 11.7
mayo 17 22.8
mayo 15 34.3
mayo 13 36.9
mayo 11 43.7
mayo 10 21.6
mayo 21 20.9
mayo 7 7.3
mayo 22 32.3
mayo 19 17.8
mayo 0 96.0
data.frame 的结构:
'data.frame': 17 obs. of 3 variables:
$ month : chr "mayo" "mayo" "mayo" "mayo" ...
$ A : Factor w/ 18 levels "0","7","8","9",..: 15 11 9 7 4 3 13 12 10 8 ...
$ B: num 6.9 16.3 19.8 12.6 19.5 65.4 11.7 22.8 34.3 36.9 ...
我的 ggplotting R 代码:
ggplot(data,aes(x = A, y = B)) +
geom_bar(stat="identity",size=1.5,colour = "black",fill="green")
更新
我的 R 代码有问题。级别 0 包含在月份 == "mayo" 中,不包含在月份 =="abril" 中(包含在完整数据中)。
确实没有显示0级:
ggplot(data[data$month=="abril",],aes(x = A, y = B)) +
geom_bar(stat="identity",size=1.5,colour = "black",fill="green")
谢谢
似乎对我有用
data <- data.frame(month=rep('mayo',17),
A=factor(c(20,16,14,12,9,8,18,17,15,13,11,10,21,7,22,19,0)),
B=c(6.9,16.3,19.8,12.6,19.5,65.4,11.7,22.8,34.3,36.9,43.7,21.6,20.9,7.3,32.3,17.8,96.0),
stringsAsFactors = F)
ggplot(data,aes(x = A, y = B)) +
geom_bar(stat="identity",size=1.5,colour = "black",fill="green")
str(data)
data.frame
的结构
'data.frame': 17 obs. of 3 variables:
$ month: chr "mayo" "mayo" "mayo" "mayo" ...
$ A : Factor w/ 17 levels "0","7","8","9",..: 15 11 9 7 4 3 13 12 10 8 ...
$ B : num 6.9 16.3 19.8 12.6 19.5 65.4 11.7 22.8 34.3 36.9 ...
data.frame 的结构有些问题。在您发布的情况下,您有 18 个因子水平.. 17 个观察值?
我的最终情节没有显示因子 A 的 0 水平。
Header数据:
month A B
mayo 20 6.9
mayo 16 16.3
mayo 14 19.8
mayo 12 12.6
mayo 9 19.5
mayo 8 65.4
mayo 18 11.7
mayo 17 22.8
mayo 15 34.3
mayo 13 36.9
mayo 11 43.7
mayo 10 21.6
mayo 21 20.9
mayo 7 7.3
mayo 22 32.3
mayo 19 17.8
mayo 0 96.0
data.frame 的结构:
'data.frame': 17 obs. of 3 variables:
$ month : chr "mayo" "mayo" "mayo" "mayo" ...
$ A : Factor w/ 18 levels "0","7","8","9",..: 15 11 9 7 4 3 13 12 10 8 ...
$ B: num 6.9 16.3 19.8 12.6 19.5 65.4 11.7 22.8 34.3 36.9 ...
我的 ggplotting R 代码:
ggplot(data,aes(x = A, y = B)) +
geom_bar(stat="identity",size=1.5,colour = "black",fill="green")
更新
我的 R 代码有问题。级别 0 包含在月份 == "mayo" 中,不包含在月份 =="abril" 中(包含在完整数据中)。
确实没有显示0级:
ggplot(data[data$month=="abril",],aes(x = A, y = B)) +
geom_bar(stat="identity",size=1.5,colour = "black",fill="green")
谢谢
似乎对我有用
data <- data.frame(month=rep('mayo',17),
A=factor(c(20,16,14,12,9,8,18,17,15,13,11,10,21,7,22,19,0)),
B=c(6.9,16.3,19.8,12.6,19.5,65.4,11.7,22.8,34.3,36.9,43.7,21.6,20.9,7.3,32.3,17.8,96.0),
stringsAsFactors = F)
ggplot(data,aes(x = A, y = B)) +
geom_bar(stat="identity",size=1.5,colour = "black",fill="green")
str(data)
data.frame
的结构'data.frame': 17 obs. of 3 variables:
$ month: chr "mayo" "mayo" "mayo" "mayo" ...
$ A : Factor w/ 17 levels "0","7","8","9",..: 15 11 9 7 4 3 13 12 10 8 ...
$ B : num 6.9 16.3 19.8 12.6 19.5 65.4 11.7 22.8 34.3 36.9 ...
data.frame 的结构有些问题。在您发布的情况下,您有 18 个因子水平.. 17 个观察值?