忽略R中ggplot的一个因素 [英] Ignoring one of the factors of ggplot in R

查看:197
本文介绍了忽略R中ggplot的一个因素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ggplot来绘制我的变量和属性。我使用ggplot和factor使用下面的代码:

$ $ p $ require(ggplot2)
require(reshape2)
df < - data.frame(HMn25_30 $ avg,HMn25_30 $ h)
df [3] = c(Normal,
Normal,
Normal,
正常,
正常,
正常,
正常,
正常,
正常,
正常,
离群值,
离群值,
离群值,
离群值,
离群值,
离群值,
正常,
离群值,
离群值,
正常,
正常,
离群值 ,
离群值,
正常,
正常

名称(df)[1]< - '节点25'
名称(df)[3]< - '结果'
df.m< - melt(df,names(df)[2:3],names(df)[1])
df.m $结果< - 因子(df.m $结果)
df.m $ HMn25_30.h< - strptime(as.character(df.m $ HMn25_30.h),format = %Y-%m-%d%H:%M:%S)
p < - ggplot(df.m,aes(x = HMn25_30.h,y = value,group = variable,color =变量))
p <-p + scale_shape_manual(values = c(20,22))
p <-p + geom_point(aes(shape = Results),cex = 9,color =blue3 )
p < - p + theme(axis.text.x = element_text(angle = 90,hjust = 1,size = 13,color =darkred))
p <-p + scale_color_manual值= c(红色))
p <-p + ylim(-1,8)
p <-p + theme_bw()
p <-p + xlab('Date和时间')
p <-p + ylab('Temprature')
p <-p + ggtitle(节点25的时间异常值)+主题(plot.title = element_text(lineheight = 3 ,color =black,size = 29))
p < - p + theme(legend.text = element_text(color =darkred,size = 25))
p < - p +主题(legen d.title = element_text(color =brown,size = 25))
p < - p + theme(axis.title.x = element_text(face =bold,color =darkred,size = 16),axis.text.x = element_text(angle = 90,vjust = 0.5,size = 26))
p <-p + theme(axis.title.x = element_text(face =bold,color =darkred,size = 14),axis.text.y = element_text(angle = 00,vjust = 0.5,size = 20))
p <-p + labs(x =Date-Time [UTC ] \ 2007-09-30,y =Temprature)
p < - p + theme(axis.title.y = element_text(size = rel(2.1),angle = 90))
p <-p + theme(axis.title.x = element_text(size = rel(2.1),angle = 00))
p <-p + geom_line(size = 1.9)
p

代码结果:



我只想呈现'离群值',并从图中省略'正常'因子。



示例数据:

 节点25 HMn25_30.h结果
1 0.26000000 2007-09-29 23:00:00正常
2 0.01500000 2007-09-30 00:00:00正常
3 -0.35333333 2007-09-30 01:00:00正常
4 -0.42333333 2007-09-30 02:00:00正常
5 -0.73333333 2007-09-30 03:00:00正常
6 -0.65000000 2007-09-30 04: 00:00正常
7 -0.40000000 2007-09-30 05:00:00正常
8 -0.09166667 2007-09-30 06:00:00正常
9 0.19000000 2007-09- 30 07:00:00正常
10 0.63500000 2007-09-30 08:00:00正常
11 1.05500000 2007-09-30 09:00:00异常值
12 1.26833333 2007-09 -30 10:00:00异常值
13 2.28166667 2007-09-30 11:00:00异常值
14 4.17000000 2007-09-30 12:00:00异常值
15 6.34000000 2007- 09-30 13:00:00异常值
16 6.56666667 2007-09-30 14:00:00异常值
17 6.74666667 2007-09-30 15:00:00正常
18 5.82833333 2007 -09-30 16:00:00异常值
19 6.36500000 2007-09-30 17: 00:00异常值
20 4.60333333 2007-09-30 18:00:00正常
21 4.98000000 2007-09-30 19:00:00正常
22 2.65666667 2007-09-30 20 :00:00异常值
23 4.90833333 2007-09-30 21:00:00异常值
24 5.05000000 2007-09-30 22:00:00正常
25 4.56500000 2007-09-30 23:00:00正常


解决方案

code> subset 参数调用 geom_point 并使用plyr 。() b






$ b $ p code pre $ c $> geom_point(aes(x = HMn25_30.h,y = value,color = variable),subset =。(Results =='Outlier'))

一个不错的可重复使用的例子

  DF < -  data.frame(a = letters [1:4],b = 1:10)

库(plyr)#必须显式加载


ggplot(DF,aes(x = b ,y = b))+
geom_point(subset =。(a =='a'),color ='blue')+
geom_po int(subset =。(a =='c'),color ='green')+
geom_line()


I use ggplot to plot my variable and attributes. I am using ggplot and factor by using the following code:

require(ggplot2)
require(reshape2)
df <- data.frame(HMn25_30$avg,HMn25_30$h)
df[3] = c("Normal",   
                "Normal",
                "Normal",
                "Normal",
                "Normal",
                "Normal",
                "Normal",
                "Normal",
                "Normal",
                "Normal",
                "Outlier",
                "Outlier",
                "Outlier",
                "Outlier",
                "Outlier",
                "Outlier",
                "Normal",
                "Outlier",
                "Outlier",
                "Normal",
                "Normal",
                "Outlier",
                "Outlier",
                "Normal",
                "Normal"
)
names(df)[1] <- 'Node 25'
names(df)[3] <-'Results'
df.m <- melt(df, names(df)[2:3], names(df)[1])
df.m$Results <- factor(df.m$Results)
df.m$HMn25_30.h <- strptime(as.character(df.m$HMn25_30.h), format = "%Y-%m-%d %H:%M:%S")
p <- ggplot(df.m, aes(x = HMn25_30.h, y = value, group = variable, color = variable))
p <- p + scale_shape_manual(values=c(20,22))
p <- p + geom_point(aes(shape = Results), cex=9, color= "blue3")
p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 1, size=13,color="darkred"))
p <- p + scale_color_manual(values=c("Red"))
p <- p + ylim(-1,8)
p <- p + theme_bw()
p <- p + xlab('Date and Time') 
p <- p + ylab('Temprature') 
p <- p + ggtitle("Temporal Outliers of Node 25 ") + theme(plot.title = element_text(lineheight=3, face="bold", color="black", size=29))
p <- p + theme(legend.text = element_text(colour="darkred", size = 25))
p <- p + theme(legend.title = element_text(colour="brown", size=25))
p <- p + theme(axis.title.x = element_text(face="bold", colour="darkred", size=16),axis.text.x  = element_text(angle=90, vjust=0.5, size=26))
p <- p + theme(axis.title.x = element_text(face="bold", colour="darkred", size=14),axis.text.y  = element_text(angle=00, vjust=0.5, size=20))
p <- p + labs(x = "Date-Time [UTC] \ 2007-09-30 ", y = "Temprature  ")
p <- p + theme(axis.title.y = element_text(size = rel(2.1), angle = 90))
p <- p + theme(axis.title.x = element_text(size = rel(2.1), angle = 00))
p <- p + geom_line(size=1.9)
p

result of the code:

I would like to present only the 'Outlier' and do omit the 'Normal' factor from plot.

Sample data:

       Node 25          HMn25_30.h Results
1   0.26000000 2007-09-29 23:00:00  Normal
2   0.01500000 2007-09-30 00:00:00  Normal
3  -0.35333333 2007-09-30 01:00:00  Normal
4  -0.42333333 2007-09-30 02:00:00  Normal
5  -0.73333333 2007-09-30 03:00:00  Normal
6  -0.65000000 2007-09-30 04:00:00  Normal
7  -0.40000000 2007-09-30 05:00:00  Normal
8  -0.09166667 2007-09-30 06:00:00  Normal
9   0.19000000 2007-09-30 07:00:00  Normal
10  0.63500000 2007-09-30 08:00:00  Normal
11  1.05500000 2007-09-30 09:00:00 Outlier
12  1.26833333 2007-09-30 10:00:00 Outlier
13  2.28166667 2007-09-30 11:00:00 Outlier
14  4.17000000 2007-09-30 12:00:00 Outlier
15  6.34000000 2007-09-30 13:00:00 Outlier
16  6.56666667 2007-09-30 14:00:00 Outlier
17  6.74666667 2007-09-30 15:00:00  Normal
18  5.82833333 2007-09-30 16:00:00 Outlier
19  6.36500000 2007-09-30 17:00:00 Outlier
20  4.60333333 2007-09-30 18:00:00  Normal
21  4.98000000 2007-09-30 19:00:00  Normal
22  2.65666667 2007-09-30 20:00:00 Outlier
23  4.90833333 2007-09-30 21:00:00 Outlier
24  5.05000000 2007-09-30 22:00:00  Normal
25  4.56500000 2007-09-30 23:00:00  Normal

解决方案

You can add a subset argument to your call to geom_point and use the plyr .() function to define the subset.

eg

p + geom_point(aes(x = HMn25_30.h, y = value, colour = variable), subset = .(Results == 'Outlier'))

A nice small reproducible example

DF <- data.frame(a = letters[1:4], b = 1:10)

library(plyr) # must be explicitly loaded


 ggplot(DF, aes(x = b, y = b)) + 
  geom_point(subset = .(a == 'a'), colour = 'blue') + 
  geom_point(subset = .(a == 'c'), colour = 'green') +
  geom_line()

这篇关于忽略R中ggplot的一个因素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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