par(mfrow=c(2,3)) #把画布分为两行三列,mfrow表示以行优先填充
手工决定图形上点的位置,可用locator()函数
在图上添加数学符号和公式,可用expression()函数
将bar_netage, bar_age, bar_sex三个图绘制在三列
library("Rmisc")
multiplot(bar_netage, bar_age, bar_sex, cols = 3)
频数分布图与滑珠图
#画频数分布图
hist(sort(datax, a$freq, xlab="微博数",ylab="频数",main="微博数分布",col="red") #xlab,ylab为坐标轴标题,main表示图形主标题
#画滑珠图
library(ggplot2)
a <- count(data$pic_num)
dd <- ddply(a,.(x,freq))
p <- ggplot(dd,aes(x=x,y=freq))
p + geom_linerange(aes(ymax=freq),color='gray',ymin=0,size=1)+geom_point(aes(color=freq),size=5)+theme_bw()+
labs(title = "微博图片分布",x = "图片数",y = "频数") +scale_colour_gradient(name = "频数")+
scale_x_continuous(limits=c(0,10), s=seq(0,10,1))
#scale_colour_gradient(name = "频数")修改图例标签等同于labs(colour = "Displment")
相关系数矩阵图与散点图矩阵
library(corrplot)
M <- cor(data1)
corrplot(M,order = 'hclust')
#散点图矩阵
scatterplotMatrix(data1[,c("user_statuses_count","user_follow_count","user_urank")],diagonal = 'histogram',ellipse=TRUE,cex.main="0.5")#cex.main="0.5"表示把标题字体大小缩小为原来的50%
箱线图
result是数据,按cluster绘制出多个箱线图,看不同cluster的网龄分布
bar_age <- barplot(pack,col="lightblue")
bar_netage <- ggplot(result, aes(x = cluster, y = 网龄, fill = cluster)) +
geom_boxplot() +
theme_bw() +
labs(x = ' cluster', y = '网龄')
频数分布图,使用ggplot2,美观倒置效果
其中数据为alldata,绘制其中的“年龄”列的频数分布图
library(ggplot2)
library(stringr)
library(plyr)
library(dplyr) # group_by()
library(Rmisc) # multiplot()
fun_bar1 <- function(data, xlab, ylab, xname, yname) {
ggplot(data, aes(xlab, ylab)) +
geom_bar(stat = 'identity', fill = "#63B8FF") +
labs(x = xname, y = yname) +
coord_flip() + # 使图形倒置
theme_minimal() # ggplot图形的一种背景主题
}
user <- alldata %>% group_by(年龄) %>% dplyr::summarise(Freq = n())
p1 <- fun_bar1(data = user,
xlab = reorder(user$年龄, user$Freq),
ylab = user$Freq,
xname = '年龄', yname = 'Frequency')
饼图
#二分类变量饼图
user5 <- alldata %>% group_by(性别) %>% dplyr::summarise(Freq = n())
p <- ggplot(user5, aes(x = "", y = user5$Freq, fill = user5$性别)) +
geom_bar(stat = "identity", width = 1) +
coord_polar(theta = "y") +
labs(x = "", y = "", title = "") ## 将标签设为空