简体   繁体   English

如何在R中使用ggplot2绘制场区域图

[英]How to draw a field area plot using ggplot2 in R

I have a data frame and I wonder how to plot a line using "max" (in red) with a filled area (in blue), and then add a second line using "min" (in green). 我有一个数据框,我想知道如何使用“ max”(红色)和填充区域(蓝色)绘制一条线,然后使用“ min”(绿色)添加第二条线。 Thanks a lot. 非常感谢。 The x-axis is "x". x轴是“ x”。

df <- data.frame(
    x = c("Jan","Feb","Mar","April","May","June","July"),
    max = c(100,150,200,300,80,130,50),
    min = c(30,20,40,25,15,10,8))

I have tried the following code: 我尝试了以下代码:

df$x = 1:7
ggplot(df, aes(x)) + 
    geom_line(aes(y = max), color="red") + 
    geom_area() + 
    geom_line(aes(y = min), color = "blue")

But an error occurs: Error in eval(expr, envir, enclos) : object 'y' not found 但是发生错误: Error in eval(expr, envir, enclos) : object 'y' not found的错误: Error in eval(expr, envir, enclos) : object 'y' not found

One option is to turn x into a factor with the levels in the correct order and set a group aesthetic: 一种选择是将x转换为具有正确顺序级别的因子,并设置组的美观度:

library(ggplot2)

df = data.frame(x = c("Jan","Feb","Mar","April","May","June","July"),
                max = c(100,150,200,300,80,130,50), 
                min = c(30,20,40,25,15,10,8))

df$x <- factor(df$x, df$x, ordered = TRUE)

ggplot(df, aes(x, ymin = min, ymax = max, group = 1)) + 
  geom_ribbon(fill = 'steelblue3', alpha = 0.5) + 
  geom_line(aes(y = min), color = 'green3') + 
  geom_line(aes(y = max), color = 'red2')

A more robust option is to parse x to Date class so x-axis labels get generated automatically: 一个更健壮的选项是将x解析为Date类,以便自动生成x轴标签:

df = data.frame(x = c("Jan","Feb","Mar","April","May","June","July"),
                max = c(100,150,200,300,80,130,50), 
                min = c(30,20,40,25,15,10,8))

df$x <- as.Date(paste(substr(df$x, 1, 3), '01 2017'), format = '%b %d %Y')

ggplot(df, aes(x, ymin = min, ymax = max)) + 
  geom_ribbon(fill = 'steelblue3', alpha = 0.5) + 
  geom_line(aes(y = min), color = 'green3') + 
  geom_line(aes(y = max), color = 'red2')

Further breaks or alternative formatting can be added with scale_x_date . 可以使用scale_x_date添加更多中断或替代格式。

Maybe you are looking for geom_ribbon : 也许您正在寻找geom_ribbon

df <- data.frame(
    x = c("Jan","Feb","Mar","April","May","June","July"),
    max = c(100,150,200,300,80,130,50),
    min = c(30,20,40,25,15,10,8))

df$xpos <- seq_len(nrow(df))

ggplot(df, aes(x = xpos)) +
    geom_ribbon(aes(ymin = min, ymax = max), fill = 'blue') +
    geom_line(aes(y = max), color = 'red') +
    geom_line(aes(y = min), color = 'green') +
    scale_x_continuous(breaks = df$xpos, labels = df$x)

If you want to start first data point and remove extra space from left end, you can set expand = c(0, 0) . 如果要开始第一个数据点并从左端删除多余的空间,可以设置expand = c(0, 0) However, you would also need to expand right limit manually to avoid the x label from being cropped. 但是,您还需要手动扩展权限限制,以免裁剪x标签。

ggplot(df, aes(x = xpos)) +
    geom_ribbon(aes(ymin = min, ymax = max), fill = 'blue') +
    geom_line(aes(y = max), color = 'red') +
    geom_line(aes(y = min), color = 'green') +
    scale_x_continuous(breaks = df$xpos, labels = df$x,
                       expand = c(0, 0), limits = c(1, 7.2))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM