ggplot:一个中的几个箱线图不起作用

时间:2019-01-23 15:12:58

标签: r

我有一个数据框,其中包含7种邻苯二甲酸盐代谢物的浓度(MEHP HMEHP OMEHP MEP MBZP MIBP MNBP),以及一列包含人口(母亲,父亲,儿童)的列。我想创建一个x轴上邻苯二甲酸酯代谢物浓度的图,并且在该图内每个代谢物三个框图显示母亲,父亲和孩子的浓度。 数据如下:

  MEHP HMEHP OMEHP MEP MBZP MIBP MNBP population
1   27    60    34  28   23   46   39    mothers
2    1   2.1   2.1 8.5  1.3   14  8.6    mothers
3  2.8    11   5.3  16  1.8   17   21    mothers
4  3.9    63    35 120   14   93  120    mothers
5  4.6    19   9.2 9.8  3.2   25   22    mothers
6  1.2   6.2   4.9  34  1.8   17  9.2    mothers

我首先融化了数据,然后尝试绘制它

df.m <- melt(data2, id.var = "population")
df.m

  population variable value
1    mothers     MEHP    27
2    mothers     MEHP     1
3    mothers     MEHP   2.8
4    mothers     MEHP   3.9
5    mothers     MEHP   4.6
6    mothers     MEHP   1.2

ggplot(data=df.m, aes(x=variable, y=log(value) +  geom_boxplot(aes(fill=population))  

我遇到了错误

Error: unexpected symbol in:
"ggplot(data=df.m, aes(x=variable, y=log(value) + geom_boxplot(aes(fill=population))   
ggplot"

我不知道为什么它不起作用。谢谢!

更新 在y = value工作后添加))。 不幸的是,它给了我错误

> ggplot(data=df.m, aes(x=variable, y=log(value))) +  geom_boxplot(aes(fill=population))
Error in Math.factor(value) : ‘log’ not meaningful for factors

> str(df.m)
'data.frame':   2499 obs. of  3 variables:
 $ population: Factor w/ 3 levels "children","fathers",..: 3 3 3 3 3 3 3 3 3  3 ...
 $ variable  : Factor w/ 7 levels "MEHP","HMEHP",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value     : Factor w/ 317 levels "#N/A","0.25",..: 58 27 53 68 77 29 44     118 38 115 .

我该如何解决?

更新

data2 <- read.csv2("190123_phthalates_population.csv", header=T,  stringsAsFactors=F, sep=";")
head(data2)
str(data2)
> str(data2)
'data.frame':   357 obs. of  8 variables:
 $ MEHP      : chr  "27" "1" "2.8" "3.9" ...
 $ HMEHP     : chr  "60" "2.1" "11" "63" ...
 $ OMEHP     : chr  "34" "2.1" "5.3" "35" ...
 $ MEP       : chr  "28" "8.5" "16" "120" ...
 $ MBZP      : chr  "23" "1.3" "1.8" "14" ...
 $ MIBP      : chr  "46" "14" "17" "93" ...
 $ MNBP      : chr  "39" "8.6" "21" "120" ...
 $ population: chr  "mothers" "mothers" "mothers" "mothers" ...

df.m <- melt(data2, id.var = "population")
head(df.m)
  population variable value
1    mothers     MEHP    27
2    mothers     MEHP     1
3    mothers     MEHP   2.8
4    mothers     MEHP   3.9
5    mothers     MEHP   4.6
6    mothers     MEHP   1.2
str(df.m)
'data.frame':   2499 obs. of  3 variables:
 $ population: chr  "mothers" "mothers" "mothers" "mothers" ...
 $ variable  : Factor w/ 7 levels "MEHP","HMEHP",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value     : Factor w/ 317 levels "#N/A","0.25",..: 58 27 53 68 77 29 44     118 38 115 ...

as.numeric(as.character(df.m$value))
Warning message:
NAs introduced by coercion 
na.omit(df.m)
> str(df.m)
'data.frame':   2499 obs. of  3 variables:
 $ population: chr  "mothers" "mothers" "mothers" "mothers" ...
 $ variable  : Factor w/ 7 levels "MEHP","HMEHP",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value     : Factor w/ 317 levels "#N/A","0.25",..: 58 27 53 68 77 29 44 118 38 115 ...
ggplot(data=df.m, aes(x=variable, y=(value)) +   geom_boxplot(aes(fill=population)))

Error: Mapping should be created with `aes() or `aes_()`.

它仍然无法正常工作

通过将excel文件中的值从常规值更改为数值,另存为csv并再次加载来更新它。

0 个答案:

没有答案