2016-08-01 103 views
0

我有如下的R命令。使用ggplot2进行R条件制图

library(ggplot2) 
df <- read.csv(file="c:\\query.csv")) 
ggplot(df) +) 
    geom_point(aes(Time, Users)) +) 
    geom_point(data=df[df$Users>30,], aes(Time, Users),) 
      pch=21, fill=NA, size=4, colour="red", stroke=1) +) 
    theme_bw()) 


在上面的命令中使用的CSV文件,如时间,用户,卖方等列

Time Users Sellers 
7 1  2 
7 2  4 
17 3  6 
19 4  8 
34 5  10 
35 6  12 
47 7  14 
63 7  18 
64 7  20 
80 7  22 
93 12  24 
94 13  26 

我的问题如下:
1)我们如何画一条线附加每个数据点?我已经更新了上面的命令,并且失败了。

ggplot(df) + geom_point(aes(Time, Users)) + geom_point(data=df[df$Users>30,], aes(Time, Users),pch=21, fill=NA, size=4, colour="red", stroke=1) + 
    geom_line() + theme_bw() 

2)怎样包括另一个图形在时间与用户图形卖家? 我已经通过以下方式完成了此操作。但是,图形输出是不是我所期待

ggplot(df) + 
    geom_point(aes(Time, Users)) + 
    geom_point(data=df[df$Users>30,], aes(Time, Users),pch=21, fill=NA, size=4, colour="red", stroke=1) + geom_point(aes(Time, Sellers)) + 
    geom_point(data=df[df$Sellers>10,], aes(Time, Sellers), pch=21, fill=NA, size=4, colour="red", stroke=1) + 
    theme_bw() 
+0

两点意见:第一,你有两个问题,这使得两个问题,一个也没有。其次,为了有一个可重复的例子,如果你运行'dput(df)'并将结果添加到你的问题中,它总是很有帮助。 – Qaswed

+1

结尾的括号是什么? –

回答

2

广告1)放置aes()部分在gplot部分:

ggplot(df, aes(Time, Users)) + 
geom_point() + geom_point(data = df[df$Users > 30,], pch = 21, fill = NA, size = 4, colour = "red", stroke = 1) + 
geom_line()+ 
theme_bw() 

广告2)你可以使用gridExtra包(请参阅:另一种方法为this questionthis one)。

p1 <- ggplot(df, aes(Time, Users)) + geom_point() + 
geom_point(data = df[df$Users > 10,], pch = 21, fill = NA, size = 4,colour = "red", stroke = 1)+ 
geom_line() + 
theme_bw() 

p2 <- ggplot(df, aes(Time, Sellers)) + geom_point() + 
geom_point(data = df[df$Sellers > 10,], pch = 21, fill = NA, size = 4, colour = "red", stroke = 1)+ 
geom_line()+ 
theme_bw() 

require("gridExtra") 
grid.arrange(p1, p1, ncol = 2) 
+0

非常感谢您回答这个问题。这真的有帮助。 –