在R中突出显示ggplot2图中的最小点和最大点 [英] Highlight minimum and maximum points in faceted ggplot2 graph in R

查看:1616
本文介绍了在R中突出显示ggplot2图中的最小点和最大点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用R中的内置经济学(来自 ggplot2 包)数据集,并绘制了使用以下代码在同一图表中为每个变量创建一个时间序列:

  library(reshape2)
library( ggplot2)

me < - melt(经济学,id = c(date))
ggplot(data = me)+
geom_line(aes(x = date, y = value))+
facet_wrap(〜variable,ncol = 1,scales ='free_y')

现在,我还想优化图表,对于每个系列,我想显示最小值和最大值的红点。
所以我想如果我能找到每个时间序列的最小值和最大值的坐标,我可以找到一种方法在每个时间序列的开始和结束处绘制一个红点。为此,我使用了以下代码:

  which(pce == min(economics $ pce),arr.ind = TRUE) 
which(pca == max(pca),arr.ind = TRUE)

并没有真正带领我到任何地方。
谢谢:)

解决方案

方法1:使用连接



当您想要保存已过滤的子集时,这可能很不错




  library(reshape2)
library(ggplot2)
library(dplyr)

me < - melt(经济学,id = c(date ))

me%>%
group_by(变量)%>%
汇总(min = min(value),
max = max(value) ) - > me.2

left_join(me,me.2)%>%
mutate(color = value == min | value == max)%>%
filter (color == TRUE) - > me.3

ggplot(data = me,aes(x = date,y = value))+
geom_line()+
geom_point(data = me.3,aes (x = date,y = value),color =red)+
facet_wrap(〜variable,ncol = 1,scales ='free_y')


谢谢@Gregor

  me.2 < -  me%>>%
group_by(变量)%>%
mutate(color =(min(value)== value | max(value)== value))

ggplot(data = me.2,aes(x = date,y = value))+
geom_line()+
geom_point(aes(color = color))+
facet_wrap(〜variable,ncol = 1,scales =free_y)+
scale_color_manual(values = c(NA ,red))


I am using the built-in economics (from the ggplot2 package) dataset in R, and have plotted a time-series for each variable in the same graph using the following code :

library(reshape2)
library(ggplot2)

me <- melt(economics, id = c("date"))
ggplot(data = me) + 
     geom_line(aes(x = date, y = value)) +
     facet_wrap(~variable, ncol = 1, scales = 'free_y')

Now, I further want to refine my graph, For each series, I want to display a red point for the smallest and the largest value. So I thought if I could find the co-ordinates of the min and max of each time-series, I could find a way to plot a red dot at beginning and ending of each time series. For this I used the following code :

which(pce == min(economics$pce), arr.ind = TRUE) 
which(pca == max(pca), arr.ind = TRUE)

This doesnt really lead me anywhere. Thank you:)

解决方案

Method 1: Using Joins

This can be nice when you want to save the filtered subsets


library(reshape2)
library(ggplot2)
library(dplyr)

me <- melt(economics, id=c("date"))

me %>%
  group_by(variable) %>%
  summarise(min = min(value),
            max = max(value)) -> me.2

left_join(me, me.2) %>%
  mutate(color = value == min | value == max) %>%
  filter(color == TRUE) -> me.3

ggplot(data=me, aes(x = date, y = value)) + 
  geom_line() +
  geom_point(data=me.3, aes(x = date, y = value), color = "red") +
  facet_wrap(~variable, ncol=1, scales='free_y')

Method 2: Simplified without Joins

Thanks @Gregor

me.2 <- me %>%
  group_by(variable) %>%
  mutate(color = (min(value) == value | max(value) == value))

ggplot(data=me.2, aes(x = date, y = value)) +
  geom_line() +
  geom_point(aes(color = color)) +
  facet_wrap(~variable, ncol=1, scales="free_y") +
  scale_color_manual(values = c(NA, "red"))

这篇关于在R中突出显示ggplot2图中的最小点和最大点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆