2016-08-01 107 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

非常感謝您回答這個問題。這真的有幫助。 –