本文所用代码来自于《R语言数据可视化之美》,我也是学习借鉴 。
沈益
7/18/2019
library(ggplot2)
library(RColorBrewer)
library(reshape2)
# 绘图数据
mydata <- data.frame(Cut = c("Fair", "Good", "Very Good", "premium", "Ideal"),
Price = c(4300, 3800, 3950, 4700, 3500))
# 对数据进行排序
mydata$Cut <- factor(mydata$Cut, levels = mydata$Cut[order(mydata$Price, decreasing = TRUE)])
# 绘制单数据系列柱形图
ggplot(mydata, aes(Cut, Price)) +
geom_bar(stat = "identity",
width = 0.8, colour = "black", size = 0.25,
fill = "#FC4E07", alpha = 1) +
ylim(0, 6000) +
theme(axis.title = element_text(size = 15, face = "plain", colour = "blue"),
axis.text = element_text(size = 12, face = "plain", colour = "red"))
# 读取绘图数据
mydata <- read.csv("配套资源/第3章_类别比较型图表/MultiColumn_Data.csv", check.names = FALSE,
sep = ",", na.strings = "NA", stringsAsFactors = FALSE)
# 将二维数据转为一维数据
mydata <- reshape2::melt(mydata, id.vars = "Catergory")
# 绘制双数据系列柱形图
ggplot(data = mydata, aes(Catergory, value, fill = variable)) +
geom_bar(stat = "identity", position = position_dodge(),
color = "black", width = 0.7, size = 0.25) +
scale_fill_manual(values = c("#00AFBB", "#FC4E07")) +
ylim(0, 10) +
theme(
axis.title = element_text(size = 15, face = "plain", color = "black"),
axis.text = element_text(size = 12, face = "plain", color = "black"),
legend.title = element_text(size = 14, face = "plain", color = "black"),
legend.background = element_blank(),
legend.position = c(0.88, 0.88)
)
# 读取数据
mydata <- read.csv("配套资源/第3章_类别比较型图表/StackedColumn_Data.csv", sep = ",",
na.strings = "NA", stringsAsFactors = FALSE)
# 将二维表转换为一维表
mydata <- melt(mydata, id.vars = "Clarity")
# 绘制堆积柱形图
ggplot(data = mydata, aes(variable, value, fill = Clarity)) +
geom_bar(stat = "identity", position = position_stack(),
color = "black", width = 0.7, size = 0.25) +
scale_fill_manual(values = brewer.pal(9, "YlOrRd")[c(6:2)]) +
ylim(0, 15000) +
theme(
axis.title = element_text(size = 15, face = "plain", color = "black"),
axis.text = element_text(size = 12, face = "plain", color = "black"),
legend.title = element_text(size = 14, face = "plain", color = "black"),
legend.background = element_rect(colour = "blue", fill = "skyblue", linetype = 2, size = 0.8),
legend.position = c(0.85, 0.75)
)
# 读取绘图数据
mydata <- read.csv("配套资源/第3章_类别比较型图表/StackedColumn_Data.csv",
sep = ",", na.strings = "NA", stringsAsFactors = FALSE)
# 将多维表转换为一维表
mydata <- melt(mydata, id.vars = "Clarity")
# 绘图
ggplot(mydata, aes(variable, value, fill = Clarity)) +
geom_bar(position = "fill", stat = "identity",
color = "black", width = 0.8, size = 0.25) +
scale_fill_manual(values = brewer.pal(9, "GnBu")[c(7:2)]) +
theme(
axis.title = element_text(size = 15, face = "plain", color = "black"),
axis.text = element_text(size = 12, face = "plain", color = "black"),
legend.title = element_text(size = 14, face = "plain", color = "black"),
legend.position = "right"
)
添加 coord_flip()即可将 x-y转换
# 读取绘图数据
mydata <- read.csv("配套资源/第3章_类别比较型图表/StackedColumn_Data.csv",
sep = ",", na.strings = "NA", stringsAsFactors = FALSE)
# 将多维表转换为一维表
mydata <- melt(mydata, id.vars = "Clarity")
# 绘图
ggplot(mydata, aes(variable, value, fill = Clarity)) +
geom_bar(position = "fill", stat = "identity",
color = "black", width = 0.8, size = 0.25) +
scale_fill_manual(values = brewer.pal(9, "GnBu")[c(7:2)]) +
coord_flip() +
theme(
axis.title = element_text(size = 15, face = "plain", color = "black"),
axis.text = element_text(size = 12, face = "plain", color = "black"),
legend.title = element_text(size = 14, face = "plain", color = "black"),
legend.position = "right"
)
# 读取绘图数据
mydata <- data.frame(Name=paste0("Project", 1:5),
Scale=c(35, 30, 20, 10, 5),
ARPU=c(56, 37, 63, 57, 59))
mydata$xmin <- 0
# 构造每个柱子的起点
for (i in 2:5){
mydata$xmin[i] <- sum(mydata$Scale[1:i-1])
}
# 构造每个柱子的终点
for (i in 1:5){
mydata$xmax[i] <- sum(mydata$Scale[1:i])
}
# 构造标签的位置
for (i in 1:5){
mydata$label[i] <- sum(mydata$Scale[1:i]) - mydata$Scale[i]/2
}
ggplot(mydata) +
geom_rect(aes(xmin = xmin, xmax = xmax, ymin = 0, ymax = ARPU, fill = Name),
colour = "black", size = 0.25) +
geom_text(aes(x = label, y = ARPU+3, label = ARPU), size = 4, col = "black") +
geom_text(aes(x = label, y = -7, label = Name), size = 3, col = "black", angle = 90) +
ylab("ARPU") +
xlab("scale") +
ylim(-10, 80) +
theme(
panel.background = element_rect(fill = "white", colour = NA),
panel.grid.major = element_line(colour = "grey60", size = .25, linetype = "dotted"),
panel.grid.minor = element_line(colour = "grey60", size = .25, linetype = "dotted"),
text = element_text(size = 15),
plot.title = element_text(size = 15, hjust = .5),
legend.position = "none"
)
mydata <- read.csv("配套资源/第3章_类别比较型图表/DotPlots_Data.csv",
sep = ",", na.strings = "NA", stringsAsFactors = FALSE)
mydata$sum <- rowSums(mydata[, 2:3])
mydata$City <- factor(mydata$City,
levels = mydata$City[order(mydata$sum)])
ggplot(mydata, aes(sum, City)) +
geom_point(shape = 21, size = 3, colour = "black", fill = "#FC4E07") +
theme(
axis.title = element_text(size = 13, face = "plain", color = "black"),
axis.text = element_text(size = 10, face = "plain", color = "black"),
legend.title = element_text(size = 14, face = "plain", color = "black")
)
mydata <- read.csv("配套资源/第3章_类别比较型图表/DotPlots_Data.csv",
sep = ",", na.strings = "NA", stringsAsFactors = FALSE)
mydata$sum <- rowSums(mydata[, 2:3])
mydata$City <- factor(mydata$City, levels = mydata$City[order(mydata$sum)])
ggplot(mydata, aes(sum, City)) +
geom_segment(aes(x=0, xend=sum, y = City, yend = City)) +
geom_point(shape = 21, size = 3, colour = "black", fill = "#FC4E07") +
theme(
axis.title = element_text(size = 13, face = "plain", color = "black"),
axis.text = element_text(size = 10, face = "plain", color = "black"),
legend.title = element_text(size = 14, face = "plain", color = "black")
)
mydata <- read.csv("配套资源/第3章_类别比较型图表/DotPlots_Data.csv",
sep = ",", na.strings = "NA", stringsAsFactors = FALSE)
mydata$City <- factor(mydata$City, levels = mydata$City[order(mydata$Female)])
mydata <- melt(mydata, id.vars = "City")
ggplot(mydata, aes(value, City, fill = variable)) +
geom_line(aes(group = City)) +
geom_point(shape = 21, size = 3, colour = "black") +
scale_fill_manual(values = c("red", "black")) +
theme(
axis.title = element_text(size = 13, face = "plain", color = "black"),
axis.text = element_text(size = 10, face = "plain", color = "black"),
legend.title = element_text(size = 12, face = "plain", color = "black"),
legend.background = element_blank(),
legend.position = c(0.85, 0.12)
)
mydata <- read.csv("配套资源/第3章_类别比较型图表/Slopecharts_Data1.csv",
sep = ",", na.strings = "NA", stringsAsFactors = FALSE)
colnames(mydata) <- c("continent", "1952", "1957")
left_label <- paste(mydata$continent, round(mydata$`1952`), sep = ", ")
right_label <- paste(mydata$continent, round(mydata$`1957`),sep=", ")
mydata$class <- ifelse((mydata$`1957` - mydata$`1952`) < 0, "red", "green")
p <- ggplot(mydata) +
geom_segment(aes(x=1, xend=2, y=`1952`, yend=`1957`, col=class), size=.75, show.legend=F) + #连接线
geom_vline(xintercept=1, linetype="solid", size=.1) + # 1952年的垂直直线
geom_vline(xintercept=2, linetype="solid", size=.1) + # 1957年的垂直直线
geom_point(aes(x=1, y=`1952`), size=3,shape=21,fill="grey80",color="black") + # 1952年的数据点
geom_point(aes(x=2, y=`1957`), size=3,shape=21,fill="grey80",color="black") + # 1957年的数据点
scale_color_manual(labels = c("Up", "Down"), values = c("green"="#A6D854","red"="#FC4E07")) +
xlim(.5, 2.5)
# 添加文本信息
p <- p + geom_text(label=left_label, y=mydata$`1952`, x=rep(1, NROW(mydata)), hjust=1.1, size=3.5)
p <- p + geom_text(label=right_label, y=mydata$`1957`, x=rep(2, NROW(mydata)), hjust=-0.1, size=3.5)
p <- p + geom_text(label="1952", x=1, y=1.02*(max(mydata$`1952`, mydata$`1957`)), hjust=1.2, size=5)
p <- p + geom_text(label="1957", x=2, y=1.02*(max(mydata$`1952`, mydata$`1957`)), hjust=-0.1, size=5)
p<-p+theme_void()
p
library(ggalt)
## Registered S3 methods overwritten by 'ggalt':
## method from
## grid.draw.absoluteGrob ggplot2
## grobHeight.absoluteGrob ggplot2
## grobWidth.absoluteGrob ggplot2
## grobX.absoluteGrob ggplot2
## grobY.absoluteGrob ggplot2
# 读取数据
df <- read.csv("配套资源/第3章_类别比较型图表/Slopecharts_Data2.csv")
colnames(df) <- c("continent", 2007:2013)
df2<-melt(df, id="continent")
df2$value<-as.numeric(df2$value)
df2$variable<-as.numeric(df2$variable)
left_label<-paste(df2$continent, round(df2$value),sep=", ")
right_label<-paste(df2$continent, round(df2$value),sep=", ")
left_point<-df2$value
right_point<-df2$value
class<-df2$variable
for (i in 1:nrow(df2))
{
if (df2$variable[i]!=1)
{
left_label[i]<-""
left_point[i]<-NaN
}
if (df2$variable[i]!=7)
{
right_label[i]<-""
right_point[i]<-NaN
}
if (df[df$continent==df2$continent[i],2]>df[df$continent==df2$continent[i],8])
{
class[i]<-"green"
}
else
{
class[i]<-"red"
}
}
p <- ggplot(df2) +
geom_xspline(aes(x=variable, y=value,group=continent, colour=class),size=.75) +
geom_vline(xintercept=1, linetype="solid", size=.1) +
geom_vline(xintercept=7, linetype="solid", size=.1) +
geom_point(aes(x=variable, y=left_point), size=3,shape=21,fill="grey80",color="black") +
geom_point(aes(x=variable, y=right_point), size=3,shape=21,fill="grey80",color="black") +
scale_color_manual(labels = c("Up", "Down"), values = c("green"="#FC4E07", "red"="#A6D854")) +
xlim(-4, 12)
p <- p + geom_text(label=left_label, y=df2$value, x=rep(1, NROW(df2)), hjust=1.1, size=3.5)
p <- p + geom_text(label=right_label, y=df2$value, x=rep(7, NROW(df2)), hjust=-0.1, size=3.5)
p <- p + geom_text(label="2007", x=1, y=1.02*(max(df2$value)), hjust=1.2, size=5) # title
p <- p + geom_text(label="2013", x=7, y=1.02*(max(df2$value)), hjust=-0.1, size=5) # title
p<-p+theme_void()+
theme(legend.position = "none")
p
mydata <- data.frame(a=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"), b=c(50, 60, 70, 20,90,110,30))
myAngle <- seq(-20, -340, length.out = 7)
ggplot(mydata) +
geom_bar(aes(x = a, y = b), width = 1, stat = "identity",
colour = "black", fill = "#F8766D") +
geom_text(aes(x = a, y = b-8, label = b), color = "white") +
coord_polar(theta = "x", start = 0) +
ylim(c(0, 120)) +
theme_light() +
theme(
panel.background = element_blank(),
panel.grid.major = element_line(colour = "grey80", size=.25),
axis.text.y = element_text(size = 12, colour = "black"),
axis.line.y = element_line(size = 0.25),
axis.text.x = element_text(size = 13, colour = "black", angle = myAngle)
)
df <- data.frame(item = rep(LETTERS[1:10], 5), # LETTERS 表示 26 个大写字母
score = rep(letters[1:5], each = 10), #letters 表示小写字母
value = rep((1:5), each = 10) + rnorm(50, 0, 0.5))
myAng <- seq(-20, -340, length.out = 10)
ggplot(df, aes(item, value, fill = score)) +
geom_bar(stat = "identity", color = "black", position = position_dodge(),
width = 0.7, size = 0.25) +
coord_polar(theta = "x", start = 0) +
ylim(c(-3, 6)) + # 控制极坐标显示半径
scale_fill_brewer(palette = "YlGnBu") + # 使用调色板调色
theme_light() + # 设置主题线为灰色
theme(
panel.background = element_blank(),
panel.grid.major = element_line(color = "grey80", size = .25),
axis.text.y = element_text(size = 12, color = "black"),
axis.line.y = element_line(size = 0.25),
axis.text.x = element_text(size = 13, colour = "black", angle = myAng)
)
ggplot2 提供了线性范围函数:geom_linerange(), 主要包括三个参数(x, ymin, ymax)
library(viridis)
df <- read.csv("配套资源/第3章_类别比较型图表/PloarRange_Data.csv",
sep = ",", na.strings = "NA", stringsAsFactors = FALSE)
df$date <- as.Date(df$date)
myAngle <- seq(-20, -340, length.out = 12)
ggplot(df, aes(date,
ymin = min.temperaturec,
ymax = max.temperaturec,
color = mean.temperaturec)) +
geom_linerange() +
scale_color_viridis("Temperature", option = "D") + # 为连续变量付颜色
ylim(-10, 35) +
coord_polar() +
theme_light() +
scale_x_date(date_breaks = "month", date_labels = "%m") + # 设置坐标轴标签为月份
theme(panel.background = element_blank(),
panel.grid.major = element_line(color = "grey80", size = .25),
axis.text.y = element_text(size = 12, color = "black"),
axis.line.y = element_line(size = .25),
axis.text.x = element_text(size = 13, color = "black", angle = myAngle))
mydata <- data.frame(car = c("Math", "English", "Biology", "Music", "R-Coding"),
value = c(12,2,14,20,18), id = c(1:5))
ggplot(mydata, aes(x = id, y = value)) +
geom_polygon(color = "black", fill = brewer.pal(7, "Set1")[3], alpha = 0.1) +
geom_point(size = 5, shape = 21, color = "black", fill = brewer.pal(7, "Set1")[1]) +
coord_polar()
label_data<-data.frame(
car=c("biology" , "english" ,"math" , "music" , "R-coding" ),
id=c(1:5) ,
v1=sample( 0:20,5, replace=T),
v2=sample( 0:20,5, replace=T)
)
AddRow<-c(NA,nrow(label_data)+1,label_data[1,ncol(label_data)-1],label_data[1,ncol(label_data)])
mydata<-rbind(label_data,AddRow)
myAngle<- 360- 360 * (label_data$id-1) /nrow(label_data)
mydata<-melt(mydata,id=c("car", "id"))
ggplot(data=mydata,aes(x=id, y=value,group=variable,fill=variable)) +
geom_polygon(colour="black",alpha=0.1)+
geom_point(size=4,shape=21,color = 'black')+
#coord_radar()+
coord_polar() +
scale_x_continuous(breaks =label_data$id,labels=label_data$car)+
theme_bw() +
ylim(0,22)+
theme(axis.text.x=element_text(size = 11,colour="black",angle = myAngle),
axis.title=element_text(size=15,face="plain",color="black"),
axis.text = element_text(size=12,face="plain",color="black"),
panel.grid.major = element_line(color="grey80"),
axis.line = element_line(color="black"),
axis.ticks = element_line(color="black"))