【问题标题】:Shade in R using ggplot2 in R when given a range [duplicate]给定范围时,在 R 中使用 ggplot2 在 R 中着色 [重复]
【发布时间】:2020-01-23 09:42:09
【问题描述】:

我有以下代码

p <- ggplot(data = Speed_results,
            aes(x = ln_sample_size, y = max_speed, group = func, colour = func)) +
  # blue plot
  #geom_point(data=Speed_results, aes(x=ln_sample_size, y=max_speed), colour="darkblue", size=1) + 
  geom_line(data=Speed_results, aes(x=ln_sample_size, y=max_speed), size=1, linetype = "dashed") + 

  geom_point(data=Speed_results, aes(x=ln_sample_size, y=median_speed),  size=1) + 
  geom_line(data=Speed_results, aes(x=ln_sample_size, y=median_speed),  size=1) + 

  geom_point(data=Speed_results, aes(x=ln_sample_size, y=min_speed), size=1) + 
  geom_line(data=Speed_results, aes(x=ln_sample_size, y=min_speed),  size=1, linetype = "dashed") + 

  # red plot
  geom_point(data=Speed_results, aes(x=ln_sample_size, y=max_speed),  size=1) + 
  geom_line(data=Speed_results, aes(x=ln_sample_size, y=max_speed),  size=1, linetype = "dashed") + 

  geom_point(data=Speed_results, aes(x=ln_sample_size, y=median_speed), size=1) + 
  geom_line(data=Speed_results, aes(x=ln_sample_size, y=median_speed),  size=1) + 

  geom_point(data=Speed_results, aes(x=ln_sample_size, y=min_speed),  size=1) +
  geom_line(data=Speed_results, aes(x=ln_sample_size, y=min_speed), size=1, linetype = "dashed") +

我想使用 R 中的 ggplot2 功能绘制 y_minimum 和 y_maximum 以及通过同一图表上的线连接的点,以获得相应的 x 值和从 y_minimum 到 y_maximum 范围的阴影。 这是graph with dummy values 。需要在粉红色虚线之间着色。需要再次在蓝色虚线之间着色。请帮忙。谢谢。

我的数据框看起来很像:

>Speed_results
# A tibble: 12 x 5
   func   ln_sample_size max_speed median_speed min_speed
   <chr>           <dbl>     <dbl>        <dbl>     <dbl>
 1 group1              2      7.01         6.8       6.61
 2 group2              2     11.4          8.93      8.24
 3 group1              3      9.86         8.60      8.4 
 4 group2              3     16.6         12.8      11.9 
 5 group1              4     28.3         26.5      26.1 
 6 group2              4     53.6         46.3      45.2 
 7 group1              5      6.47         6.45      6.45
 8 group2              5      6.89         6.68      6.65
 9 group1              6      8.48         8.25      8.23
10 group2              6     30.6         13.0      12.5 
11 group1              7     27.6         26.2      26.1 
12 group2              7    110.          86.1      78.1 

> dput(Speed_results)
structure(list(func = c("group1", "group2", "group1", "group2", 
"group1", "group2", "group1", "group2", "group1", "group2", "group1", 
"group2"), ln_sample_size = c(2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 
7), max_speed = c(7.00625, 11.375, 9.8625, 16.5875, 28.26875, 
53.6125, 6.46558125, 6.88586875, 8.47971875, 30.60874375, 27.55455625, 
110.140775), median_speed = c(6.8, 8.934375, 8.603125, 12.80625, 
26.5, 46.334375, 6.45095625, 6.675790625, 8.25186875, 12.96550625, 
26.213890625, 86.12590625), min_speed = c(6.6125, 8.2375, 8.4, 
11.875, 26.1, 45.24375, 6.44804375, 6.653425, 8.23073125, 12.45396875, 
26.12114375, 78.09409375)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))

【问题讨论】:

  • 嗨,多维尼·贾亚辛格。你能告诉我们你的尝试和它的问题吗?
  • 是的。让我添加我的图表。我需要的是红线和绿线之间的阴影
  • 这能回答你的问题吗? Fill area between multiple lines in plot
  • 谢谢。之前没找到。我遇到的是填充曲线下的区域。
  • 部分是的。它确实

标签: r ggplot2


【解决方案1】:

您可以使用以下代码(从 https://stackoverflow.com/a/48205302/6123824 获取帮助)

library(tidyverse)
data.frame(x, y_minimum,y_maximum) %>% #combine the three data frames
  group_by(x) %>% # group by x for next step
  mutate(max = max(y_minimum, y_maximum), # calculate max of y_minimum, y_maximum
         min = min(y_minimum, y_maximum)) %>% #calculate min of y_minimum, y_maximum
  gather(key, value, 2:3) %>% #convert to long format so you can color by key in the geom line call
  ggplot()+
  geom_ribbon(aes(x = x, ymin = min, ymax = max), fill= "red", alpha = 0.3)+
  geom_line(aes(x = x, y = value, color = key))

更新

数据

df = structure(list(func = c("group1", "group2", "group1", "group2", 
        "group1", "group2", "group1", "group2", "group1", "group2", "group1", 
        "group2"), ln_sample_size = c(2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 
        7), max_speed = c(7.00625, 11.375, 9.8625, 16.5875, 28.26875, 
        53.6125, 6.46558125, 6.88586875, 8.47971875, 30.60874375, 27.55455625, 
        110.140775), median_speed = c(6.8, 8.934375, 8.603125, 12.80625, 
        26.5, 46.334375, 6.45095625, 6.675790625, 8.25186875, 12.96550625, 
        26.213890625, 86.12590625), min_speed = c(6.6125, 8.2375, 8.4, 
        11.875, 26.1, 45.24375, 6.44804375, 6.653425, 8.23073125, 12.45396875, 
        26.12114375, 78.09409375)), row.names = c(NA, -12L), class = c("tbl_df", 
        "tbl", "data.frame"))

ggplot2绘图

ggplot(data=df, aes(y=median_speed, x=ln_sample_size, group=func, colour=func,
                          fill=func)) +
  geom_point() +
  geom_line() +
  geom_ribbon(aes(ymin=min_speed, ymax=max_speed), alpha=.3, linetype=0) +
  theme_bw()+
  labs(x = "x label",
       y = "y label",
       title = "Title")

【讨论】:

  • 这对我有部分帮助。我在同一个情节中有两套。当我用(比如说)红色添加 geom_ribbon 命令时,接下来的 2 行(我总共有 4 行 - 两行红色和两行蓝色)再次使用蓝色的命令都被红色阴影。我怎样才能用两种不同的颜色着色呢?
  • 没有您的数据,就无法为您提供帮助。您可以提供dput()格式的数据。
  • 我的列名是,ln_sample_size,min_speed,max_speed,median_speed,func。 ln_sample_size 是 X 轴。 func 有两个类别(比如第 1 组和第 2 组)。其余列作为 Y 轴值。
  • 在您提供的答案中,假设粉色阴影区域代表第 1 组。我需要为同一张图中的第 2 组设置另一种类似的阴影区域(例如蓝色)。
  • 请查看此链接以查看我的数据框模式。 drive.google.com/file/d/19NpxgSeea0qunsRd_WWi3mW-g6p-rTJN/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
相关资源
最近更新 更多