【问题标题】:How to create a faceted graph with multiple Min and Max points that are grouped如何创建具有分组的多个最小和最大点的多面图
【发布时间】:2019-12-04 14:24:29
【问题描述】:

我想创建一个包含多个minmax 点的图表,这些点按monthyear 分组。

我的数据集trythis3

structure(list(Month = structure(c(5L, 4L, 3L, 2L, 1L, 2L, 3L, 
4L), .Label = c("Feb", "Apr", "Jun", "Aug", "Oct"), class = "factor"), 
    Year = c(1994L, 1995L, 1996L, 1997L, 1998L, 2003L, 2007L, 
    2011L), Temperature = c(10.1717660661212, 19.1113251384721, 
    14.80103145439, 8.59164858808079, 6.86800188715349, 9.55507283803179, 
    15.6577982450023, 16.25518817037), color = c(FALSE, FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, TRUE), color1 = c(FALSE, 
    FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE)), row.names = c(NA, 
-8L), groups = structure(list(Year = c(1994L, 1995L, 1996L, 1997L, 
1998L, 2003L, 2007L, 2011L), .rows = list(1L, 2L, 3L, 4L, 5L, 
    6L, 7L, 8L)), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

data.frame 的身份对此进行调查:

  Month  Year Temperature color color1
  <fct> <int>       <dbl> <lgl> <lgl> 
1 Oct    1994       10.2  FALSE FALSE 
2 Aug    1995       19.1  FALSE FALSE 
3 Jun    1996       14.8  FALSE FALSE 
4 Apr    1997        8.59 FALSE FALSE 
5 Feb    1998        6.87 FALSE FALSE 
6 Apr    2003        9.56 FALSE FALSE 
7 Jun    2007       15.7  FALSE FALSE 
8 Aug    2011       16.3  TRUE  FALSE 

我是如何计算 colcolorcolor1 的,它们是变量 MonthYearminmax 值:

trythis1 <- prac1 %>%
  group_by(Month) %>%
  mutate(color = (min(Temperature) == Temperature | max(Temperature) == Temperature))
trythis2 <- prac1 %>%
  group_by(Year) %>%
  mutate(color = (min(Temperature) == Temperature | max(Temperature) == Temperature))
trythis3 <- cbind(trythis1, trythis2)
trythis3 <- trythis3[, c(-5, -6, -7)]

data.frameprac1 的示例:

    Month Year Temperature
1     Jan 1994  5.11379276
2     Feb 1994  3.21683318
3     Mar 1994  7.78435180
4     Apr 1994  8.37781038
5     May 1994 10.97117575
6     Jun 1994 15.06396991
7     Jul 1994 19.06792551
8     Aug 1994 16.84395137
9     Sep 1994 13.28449477
10    Oct 1994 10.17176607
11    Nov 1994  9.87512853
12    Dec 1994  6.23880411
13    Jan 1995  4.39990197
14    Feb 1995  6.37256581
15    Mar 1995  5.57871938
16    Apr 1995  9.09780864
17    May 1995 11.83570263
18    Jun 1995 13.85246862
19    Jul 1995 19.19339040
20    Aug 1995 19.11132514
21    Sep 1995 13.90945926

我认为它如何与这段代码一起工作:

ggplot(trythis3, aes(x = Month, Temperature, group = Year)) + geom_line() + geom_point(aes(color = color)) + geom_point(aes(color = color1)) + facet_wrap(~Year) + scale_color_manual(values = c(NA,c("red", "blue")))

输出: 这应该是它的样子(为油漆编辑道歉):

【问题讨论】:

  • 为绘图使用两个数据框。线使用prac1,点图层使用长格式数据,其中一列表示“类型”,值为“最大月”、“最小月”和“最大年”。
  • 我认为这里缺少的一件事是制作您对映射感兴趣的类别。您有 5 个要为其设置颜色的类别(如果您计算 NA),但您只创建了两个类别(TRUE 和 FALSE)。你可以用case_when()(或嵌套的ifelse())来做到这一点。

标签: r ggplot2 graphics


【解决方案1】:

为绘图使用两个数据框。线使用 prac1,点图层使用长格式数据,其中一列表示“类型”,其值为“最大月”、“最小月”和“最大年”:

year_summ = prac1 %>%
  group_by(Year) %>%
  arrange(Temperature) %>%
  slice(1, n()) %>%
  mutate(type = c("Min Year", "Max Year"))

month_summ = prac1 %>%
  group_by(Month) %>%
  arrange(Temperature) %>%
  slice(1, n()) %>%
  mutate(type = c("Min Month", "Max Month"))

prac1_summ = bind_rows(year_summ, month_summ) %>%
  mutate(type = factor(type, levels = c("Min Month", "Max Month", "Min Year", "Max Year"))) %>%
  arrange(type)

ggplot(prac1, aes(x = Month, Temperature, group = Year)) + 
  geom_line() +
  geom_point(data = prac1_summ, aes(color = type)) +
  scale_color_manual(values = c("red", "blue", "yellow", "green")) +
  facet_wrap(~Year)


使用这些数据:

prac1 = read.table(text  = "    Month Year Temperature
1     Jan 1994  5.11379276
2     Feb 1994  3.21683318
3     Mar 1994  7.78435180
4     Apr 1994  8.37781038
5     May 1994 10.97117575
6     Jun 1994 15.06396991
7     Jul 1994 19.06792551
8     Aug 1994 16.84395137
9     Sep 1994 13.28449477
10    Oct 1994 10.17176607
11    Nov 1994  9.87512853
12    Dec 1994  6.23880411
13    Jan 1995  4.39990197
14    Feb 1995  6.37256581
15    Mar 1995  5.57871938
16    Apr 1995  9.09780864
17    May 1995 11.83570263
18    Jun 1995 13.85246862
19    Jul 1995 19.19339040
20    Aug 1995 19.11132514
21    Sep 1995 13.90945926", header = T)

prac1$Month = factor(prac1$Month, levels = month.abb)

【讨论】:

  • 如果我想创建范围条而不是指示最小值和最大值的点,该怎么做?此外,我一直试图通过绘制的pointYearMonthminmax 值包含在图表上。我使用了geom_text(aes(label=round(Temperature), 2)),但它们只是聚集在一个point 在所有图表的同一位置,这可以用箭头或线指示它的位置吗?
  • 也许用另一个油漆模型问一个关于错误栏的新问题。你想要什么 x 位置的温度?
  • 对于文本,请确保您使用的是相同的 prac1_summ 数据框。它应该继承xy 位置的最新美学。还要确保你没有遗漏括号...geom_text(aes(label=round(Temperature, 2)))
  • 啊,是的!否则,我将尝试自己计算我们的误差线图,如果尚未回答,这可能是一个未来的问题。通过将data=prac1_summ 添加到geom_text 上就可以了。但是,round 不起作用,所以我不得不通过这样做将Temperaturedata.frame prac1_summ 舍入:prac1_summ &lt;- ddply(prac1_summ, .(Month, Year, type), summarise, Temperature=round(Temperature,2)) 我唯一的问题是数据标签现在位于点上而不是靠近它们.有没有办法用一条指向彩色圆点的线将它们定位在它们周围?
  • Round 应该可以正常工作,你可能有一个小的语法错误。将geom_text(data = prac1_summ, aes(label=round(Temperature, 2))) + 添加到我的答案中的代码可以正常工作。使用 nudge 参数调整标签。如果你想要带有线条/箭头的标签,我认为 directlabels 包可以做到这一点,但我不熟悉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 2023-01-16
  • 2021-08-25
相关资源
最近更新 更多