geom_boxplot:将“character”标签放在一个连续的x轴上,矩形(geom_rect)作为背景

时间:2016-08-06 12:11:11

标签: r plot ggplot2 boxplot axis-labels

我想为时间序列创建一个boxplot,我可以在其中使用矩形(geom_rect)为backgroudn着色,并将标签指定为字符。

我可以绘制箱线图,但由于我需要连续比例来指定矩形,我无法将标签绘制为字符

这里我把我写的代码:

1:创建数据集

# load packages
library (dplyr)
library (ggplot2)
Order <- rep (seq (1:24), 100)
Values <- runif (2400, 0, 1)
Season <- rep ( (rep (c( rep("Spring", 3), rep ("Summer", 3), rep ("Autumn", 3), rep ("Winter", 3)),2)), 100)
Date <- rep( (c("D-70", "J-71", "F-71", "M-71", "Ap-71", "May-71", "Jun-71", "Jul-71", "Au-71", "S-71", "O-71", "N-71",
          "D-71", "J-72", "F-72", "M-72", "Ap-72", "May-72", "Jun-72", "Jul-72", "Au-72", "S-72", "O-72", "N-72")), 100)
df <- data.frame (Order, Values, Season, Date) ; head (df)

定义我想用矩形着色的句点

# Indicate the seasons for the X axis
Seasons_plot <- df %>% distinct(Date) %>% arrange (Order) # Select the position of each season for plotting it
start_spring <- which(Seasons_plot$Season == "Spring") ; end_spring <- start_spring + 1
start_summer <- which(Seasons_plot$Season == "Summer") ; end_summer <- start_summer + 1
start_autumn <- which(Seasons_plot$Season == "Autumn") ; end_autumn <- start_autumn + 1
start_winter <- which(Seasons_plot$Season == "Winter") ; end_winter <- start_winter + 1

n <- max (length(start_spring), length(start_summer), length(start_autumn), length(start_winter),
          length(end_spring), length(end_summer), length(end_autumn), length(end_winter))

length(start_spring) <- n ; length(start_summer) <- n ; length(start_autumn) <- n; length(start_winter) <- n
length(end_spring) <- n; length(end_summer) <- n; length(end_autumn) <- n; length(end_winter) <- n

#Indicate the size (y axis) of the rectangle
ymin_season <- rep(0, n)
ymax_season <- rep(1, n)

# Create a dataframe for indicating the position of the rectangles
shade <- data.frame (start_spring, end_spring, 
                     start_summer, end_summer, 
                     start_autumn, end_autumn, 
                     start_winter, end_winter, 
                     ymin_season, ymax_season)

选择要绘制的标签以便于可视化(例如每4个1)

labels <- Seasons_plot$Date 
labels
# Select the number of the labels to plot
labels_selected <- seq(from=1, to=length(labels), by=4)
labels_plot <- c()
for (i in 1:length(labels)){
  if (i %in% labels_selected){
    labels_plot[i] <-  as.character (labels [i])
  } else {
    labels_plot[i] <- c(" ")
  }
}
labels_plot

绘制箱线图

library (plyr)
ggplot (df) +
  geom_boxplot(aes(Order, Values, group=round_any(Order, 1)), outlier.colour = NA) +
  geom_rect (data=shade, aes(xmin=start_spring, xmax=end_spring, ymin=ymin_season, ymax=ymax_season), fill="green", alpha=0.05) +
  geom_rect (data=shade, aes(xmin=start_summer, xmax=end_summer, ymin=ymin_season, ymax=ymax_season), fill="blue", alpha=0.05) +
  geom_rect (data=shade, aes(xmin=start_autumn, xmax=end_autumn, ymin=ymin_season, ymax=ymax_season), fill="orange", alpha=0.05) +
  geom_rect (data=shade, aes(xmin=start_winter, xmax=end_winter, ymin=ymin_season, ymax=ymax_season), fill="white", alpha=0.05) +
  scale_x_discrete (labels=labels_plot)
detach("package:plyr", unload=TRUE)

你知道如何把标签作为角色放在这个情节中吗?或者我如何混合两个图(一个用于矩形,另一个用于带有标签的箱图)?

提前致谢,

1 个答案:

答案 0 :(得分:1)

我认为你的问题是如果你调整了df $ Order是class integer

 df$Order <- as.factor(df$Order)

您现有的代码就像魅力一样。 (我删除了group = plyr::round_any(Order, 1))PS package::function语法是使用特定包函数的好方法,而不需要处理包加载/卸载。这就是我跑的方式:

df$Order <- as.factor(df$Order)

ggplot (df) +
  geom_boxplot(aes(Order, Values),outlier.colour = NA) +
  geom_rect (data=shade, aes(xmin=start_spring, xmax=end_spring,
    ymin=ymin_season, ymax=ymax_season), fill="green", alpha=0.05) +
  geom_rect (data=shade, aes(xmin=start_summer, xmax=end_summer,
    ymin=ymin_season, ymax=ymax_season), fill="blue", alpha=0.05) +
  geom_rect (data=shade, aes(xmin=start_autumn, xmax=end_autumn,
    ymin=ymin_season, ymax=ymax_season), fill="orange", alpha=0.05) +
  geom_rect (data=shade, aes(xmin=start_winter, xmax=end_winter,
    ymin=ymin_season, ymax=ymax_season), fill="white", alpha=0.05) +
  scale_x_discrete(labels=labels_plot)

这种美貌突然出现了:

enter image description here