因此,在线查看ggplot2示例时,似乎所有使用的数据都被构造为不同的数据观察(如行)和列中的一致属性。
e.g。
head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
现在,我试图将我个人组织的数据图表作为行的日期和列的总统候选人。观察结果是他们在民意调查中投票的总体百分比。
数据看起来像这样
load(url("http://www.clutchmemes.com/Random/GOPArray.RData"))
require(plyr)
averagedGOPPoll <- as.data.frame(aaply(allGOP, 1:2, mean, na.rm=TRUE))
averagedGOPPoll <- cbind(dates=as.Date(rownames(averagedGOPPoll)), veragedGOPPoll)
head(averagedGOPPoll)
dates Trump Carson Rubio Cruz Bush Rand.Paul Christie Fiorina
2015-01-01 2015-01-01 NaN NaN NaN NaN NaN NaN NaN NaN
2015-01-02 2015-01-02 NaN NaN NaN NaN NaN NaN NaN NaN
2015-01-03 2015-01-03 NaN NaN NaN NaN NaN NaN NaN NaN
2015-01-04 2015-01-04 NaN NaN NaN NaN NaN NaN NaN NaN
2015-01-05 2015-01-05 NaN NaN NaN NaN NaN NaN NaN NaN
2015-01-06 2015-01-06 NaN NaN NaN NaN NaN NaN NaN NaN
我怎样才能以线性方式绘制这个图表,其中y轴为日期,每个候选人的行数不同,以及x轴的总投票率。
这样的事情:(取自赫芬顿邮报) 我的问题是独特的,因为我需要一种非常强大的方式来组织这样的结构化数据,因为我将把它与民意调查中的数据以及Google趋势数据相结合。因为把它煮到我所拥有的地方相对容易,我需要一种系统的方法来解决这个问题。位置。所以,是的,我的问题是独一无二的,答案也非常适合我的问题。不知道为什么这是重复的。
答案 0 :(得分:3)
您需要“融化”您的数据框。例如,您可以使用reshape2包:
### load pakages
library(plyr)
library(reshape2)
library(ggplot2)
### load data
load(url("http://www.clutchmemes.com/Random/GOPArray.RData"))
### convert to data frame
averagedGOPPoll <- as.data.frame(aaply(allGOP, 1:2, mean, na.rm=TRUE))
### add date column
averagedGOPPoll$date = as.Date(rownames(averagedGOPPoll))
### reshape data frame
averagedGOPPoll.melt <- melt(averagedGOPPoll,id=c('date'))
names(averagedGOPPoll.melt) = c('date','candidate','percentage')
### plot
ggplot(averagedGOPPoll.melt,aes(x=date,y=percentage,colour=candidate))+
geom_line()
我认为其他选项是一个名为tidyr的新库:
### load pakages
library(plyr)
library(tidyr)
library(ggplot2)
### load data
load(url("http://www.clutchmemes.com/Random/GOPArray.RData"))
### convert to data frame
averagedGOPPoll <- as.data.frame(aaply(allGOP, 1:2, mean, na.rm=TRUE))
### add date column
averagedGOPPoll$date = as.Date(rownames(averagedGOPPoll))
### reshape data frame
averagedGOPPoll.melt <- gather(averagedGOPPoll,candidate,percentage,-date)
### plot
ggplot(averagedGOPPoll.melt,aes(x=date,y=percentage,colour=candidate))+
geom_line()