如何清理这个数据框以使用ggplot绘制一个boxplot?

时间:2018-05-20 22:13:13

标签: r boxplot

enter image description here

数据框包含8种颜色,每种颜色有1000个值。我想用ggplot绘制8个盒子的箱形图。但是如何在ggplot中设置x和y值?非常感谢你!

1 个答案:

答案 0 :(得分:1)

由于没有提供样本数据集,因此调整起来有点困难,所以我提供了一个包含4个值的样本,每个颜色都有1000个值。

library(tidyverse)
colorrs <- c("red","green","blue","orange")#load the 4 colors
colordata <- tibble::tibble(color = rep(colorrs, 1000),#make a tibble
               values = rnorm(4000))

#plot normally distributed values by color and adjust the colors 
colordata %>% 
  #Don't forget to set the aes values. X is first, y is second
   ggplot(aes(color,values,fill = color))+
   #geom_boxplot to create boxplots
   geom_boxplot()+
  scale_fill_manual(values = c("blue","green","orange","red"))