我的数据框具有以下名为dat的结构:
No. Action Title Replies PlusOnes Reshares Url
1 Share SomeTitle 2 3 1 http://plus.google.com
2 Post AnotherTitle 3 4 5 http://plus.google.com
这是dput(dat)
我的数据:
structure(list(No. = 1:2, Action = c("Share", "Post"), Title = c("Some Title",
"Another Title"), Replies = 2:3, PlusOnes = 3:4, Reshares = c(1L,
5L), Url = c("http://plus.google.com", "http://plus.google.com"
)), .Names = c("No.", "Action", "Title", "Replies", "PlusOnes",
"Reshares", "Url"), class = "data.frame", row.names = c(NA, -2L
))
我怎样才能绘制列Replies
,PlusOnes
并Reshares
以图表?x轴应为No.
,y轴应为Replies / PlusOnes / Reshares的数量,并且这些列在图形中应各有一条单独的线。
问候
您可以通过两种方式执行此操作:使用标准R图形或ggplot2(我更喜欢)。
既然另一个答案说明了如何使用matplot
,那么我将向您展示另一种获取漂亮图形的方法。
require(ggplot2)
require(reshape2)
dat<-data.frame('No.'=1:2,replies=2:3,PlusOnes=3:4,Reshares=c(1,5))
melted=melt(dat,id.vars='No.')
ggplot(melted,aes(x=factor(No.),y=value,color=factor(variable),group=factor(variable)))+
geom_line()+xlab('No.')+guides(color=guide_legend("Series"))+
labs(title="Insert Title Here")
从本质上讲,发生的事情是melt()
reshape2包中的函数将为每个级别的每个可能的列值创建一个新的行条目。No.
它还在每个行中存储与之对应的值的变量名,因此很容易绘制类似它的内容与ggplot。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句