Ignoring one of the factors of ggplot in R

对着背影说爱祢 提交于 2019-12-06 15:32:50

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()

Use the subset of the data frame where Results == "Outlier" for the geom_point command:

p <- p + geom_point(data = df.m[df.m$Results == "Outlier",], 
                    cex=9, color= "blue3", shape = 22) 

Then, there's no need for the scale_shape_manual command.

The complete code:

library(ggplot2)
p <- ggplot(df.m,
            aes(x = HMn25_30.h, y = value, group = variable, color = variable))
#p <- p + scale_shape_manual(values=c(20,22))                 # command removed
p <- p + geom_point(data = df.m[df.m$Results == "Outlier",], 
                    cex=9, color= "blue3", shape = 22)        # command modified
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

You have not supplied reproducible code that includes your data, but it seems likely you can achieve this with simple subsetting. Try changing this line:

p <- ggplot(df.m, aes(x = HMn25_30.h, y = value, group = variable,
                                         color = variable))

to this:

p <- ggplot(df.m[df.m$Result == "Outlier", ], aes(x = HMn25_30.h, y = value,
                                    group = variable, color = variable))

If I understand the structure of your data frame correctly that should plot only the rows for which the Results column has the value Outlier.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!