数据下载地址: votes and seats-2.csv
执行环境Rstudio
首先导入数据,确认简单的信息。
votes.seats-read.CSV (votesandseats-2.CSV )、na.strings=''、head=TRUE、sep='、' ) DIM ) votes.CSV 通过查看缺省的6tail(votes.seats,8 ) names ) votes.seats )列名称,您可以看到生成了两个表和一些数据。
纬度和列名称:
前十名
后8个
综上所述,na.strings=’-’的作用是原表的“-”部分已经被na表示,在head=TRUE中标题不被认为是数据,基本上是与python相同的读取方法。
然后,要单独查看某一列,请使用$操作符。 例如,votes.seats$election只显示election列中的数据。
然后,分割election列中的年月日。 这里使用了str_split。
str_split(votes.seats$election,',simplify=T ) )。
生成与日月年和选举一词分别对应的列数为4的矩阵,但这里只显示前几行。 simplify=T的作用是生成矩阵。 否则将生成列表。
然后使用多操作。 这样做是为了允许将新列添加到原始表的末尾。 当然,也可以对现有列进行修改。 只要表中存在关键字即可,不存在的关键字将单独添加列。
install.packages('tidyverse ' ) library (DP lyr ) library (stringr ) library ) tidyr ) votes.seats %% #使用了mutate函数(year=str_split(election,',simplify=T ),3 )首先下载package并导入一些库。 稍后使用。
请注意,这里需要使用%%操作符。 我个人觉得像是调用java的.操作,也就是函数。 mutate函数将新列的名称规定为year,显示内容为刚才得到的矩阵的第三列,即“年”。
可以看到vote.seats的最后一列增加了一列year。
当然,如果需要,也可以添加其他列。
在此添加一些其他列以供后续使用。 各派的投票比率是:
votes.seats-votes.seats % % mutate (year=str _ split (election,',simplify=T ),3 ),coalition.seat.) ) #百分比labor.seat.share=labor.seats/total.seats, 转换为other.seat.share=other.seats/total.seats,total.seat.share=Coalition.seat.share labor.seat.share 他的作用是实现key这样的函数。希望不同派(Coalition、Labor、Other )之间的投票比率分别与去年不同。
votes.seats%%Gather(Party,seat.share,coalition.seat.share,labor.seat.share,other.seat.share ) )
这里看不懂也没关系。 可以看出表中的行数为129。 到现在为止是43行,正好变成了3倍。 也就是说,每年为每个交易方单独生成一行,因此每年生成三行。
继续使用dplyr函数,在前面的代码中直接操作就可以了。 然后我们就
a-votes.seats%%Gather(Party,seat.share,coalition.seat.share,labor.seat.share,other.seat.share ) (显示party=DP lyr 33603360记录(party,' coalition.seat.share '=' Coalition ',' Coalition ' )。
设这个表为a。 他是关于年party,seat.share的
使用dplyr:select时,仅选择这些列的显示。
使用DP lyr :记录定义新名称。
因为这里没有序列,所以他成为了Coalition这个party,还有Labor,最后是关于Other的43个。 接下来我们会让他看起来更合理。
同样,生成另一个关于vote.share的表。
B-votes.seats%%Gather(party,vote.share,coalition.pmy,labor.pmy, other.pmy ) % DP lyr :3360 select vote.share % #此语句仅用于选择mutate (party=DP lyr :3360 recode (party,' cocord
然后使用merge函数将两个表融合在一起
pmy_vote_efficiency-a%%merge(b ) pmy_vote_efficiency
现在,您可以看到此表将seat.share和vote.share合并为一个表。
接着,使用绘图函数ggplot2,可视化他们之间(seat share,vote share )的关系
library(ggplot2) library (scales ) ggplot ) pmy_vote_efficiency,AES ) x=vote.share,y=seat.share,colour
实际上,你可以把aes的内容写在后面
gplot(PMY_vote_efficiency ) geom _ point (AES ) x=vote.share,y=seat.share,colour=party ) )将绘制内容转换为
gplot(PMY_vote_efficiency,AES ) x=vote.share,y=seat.share,colour=party ) (geom_point ) ) colour=party ) ) se=F ) scale _ x _ continuous (labels=percent ) scale_y_continuous ) labels=percent ) scale (labs (title=' yourplottitlegoeshere ',x='x-axis ',y='y-axis ' ) theme_minimal ) ) theme ) lllple )
geom_abline ) )生成整体回归直线,而geom_smooth为每种类型的数据生成单独的直线。
sale _ x _ continuous (labels=percent )将坐标轴更改为百分比显示。