R语言之可视化(23)高亮某一元素

2019-03-20 10:18:10 浏览数 (1)

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中的某一元素时,首先需要新建一列,然后修改新建列中需要高亮的部分即可

0 人点赞