R语言之可视化(23)高亮某一元素
代码语言:javascript复制library(ggplot2)
data("PlantGrowth")
str(PlantGrowth)
data.frame': 30 obs. of 2 variables:
$ weight: num 4.17 5.58 5.18 6.11 4.5 4.61 5.17 4.53 5.33 5.14 ...
$ group : Factor w/ 3 levels "ctrl","trt1",..: 1 1 1 1 1 1 1 1 1 1 ...
pg <- PlantGrowth
pg$h1 <- 'trt'
pg$h1[pg$group=='ctrl'] <- 'ctrl'
ggplot(pg,aes(x = group,y = weight, fill= h1)) geom_boxplot()
scale_fill_manual(values = c('red','grey50'))
总结:假如需要高亮ggplot2中的某一元素时,首先需要新建一列,然后修改新建列中需要高亮的部分即可