ggplot2 热图
m3 <- read.csv("C:\\Users\\liangxuefeng\\Desktop\\heatmap2.csv",header = T,stringsAsFactors = F)
m3 %<>%
#因子排序
mutate(County=factor(County,levels = unique(County))) %>%
mutate(IncidenceFactor=cut(Incidence,
breaks = c(-1,0,0.1,0.5,1.0,5.0,10.0,
max(m3$Incidence,na.rm = T)),
labels = c("0","0-0.1","0.1-0.5","0.5-1.0","1.0-5.0","5.0-10.0",
">10.0")))
library(ggplot2)
ggplot(m3,aes(x = factor(Year), y = County)) +
geom_tile(aes(fill = Incidence),colour = "white",size = 0.25) +
scale_y_discrete(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0),
breaks = c("1960","1970","1980","1990","2000","2010")) +
coord_fixed() +
theme_grey(base_size = 8) +
theme(axis.text = element_text(face = "bold"),
axis.ticks = element_line(size = 0.4),
plot.background = element_blank(),
panel.border = element_blank())
textcol <- "black"
ggplot(m3,aes(x = factor(Year), y = County, fill = IncidenceFactor)) +
geom_tile(colour = "white", size = 0.25) +
labs(x = "", y= "", title = "Incidence of JE in China") +
scale_y_discrete(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0),
breaks = c("1960","1970","1980","1990","2000","2010")) +
scale_fill_manual(values = c("#d53e4f","#f46d43","#fdae61","#fee086",
"#e6f598","#ddf1da","#abdda4"),
na.value = "grey90") +
theme_grey(base_size = 8) +
theme(legend.position = "right",legend.direction = 'vertical',
legend.title = element_blank(),
legend.text = element_text(colour = textcol,
size = 6,
face = 'bold'),
legend.key.height = grid::unit(0.8,"cm"),
legend.key.width = grid::unit(0.2,"cm"),
axis.text.x = element_text(size =8,colour = textcol),
axis.text.y = element_text(vjust = 0.2,colour = textcol),
axis.ticks = element_line(size = 0.4),
plot.background = element_blank(),
panel.border = element_blank(),
plot.title = element_text(colour = textcol,
hjust = 0.5,
size = 14,
face = 'bold'))
ggplot(m3,aes(x = factor(Year), y = County, fill = factor(m3$IncidenceFactor,levels = rev(levels(m3$IncidenceFactor))))) +
geom_tile(colour = "white", size = 0.25) +
labs(x = "", y= "", title = "Incidence of JE in Gansu") +
scale_y_discrete(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0),
breaks = c("1960","1970","1980","1990","2000","2010")) +
scale_fill_manual(values = c("#d53e4f","#f46d43","#fdae61","#fee086",
"#e6f598","#ddf1da","#abdda4"),
na.value = "grey90") +
theme_grey(base_size = 8) +
theme(legend.position = "bottom",legend.direction = 'horizontal',
legend.title = element_blank(),
legend.text = element_text(colour = textcol,
size = 8,
face = 'bold'),
legend.key.height = grid::unit(0.1,"cm"),
legend.key.width = grid::unit(0.5,"cm"),
axis.text.x = element_text(size =8,colour = textcol))+
#in one row
guides(colour = guide_legend(nrow=1),fill=guide_legend(nrow=1,reverse=TRUE))
library(tidyverse)
obs<-c('a','b','c','d')
before<-c(2,4,6,8)
after<-c(4,2,8,4)
df<-cbind(before, after)
row.names(df)=obs
df <- as.data.frame(df)
df$col <- factor(df$after - df$before > 0,labels=c("descending","ascending"))
df$obs <- obs
df_l <- gather(df,key,value,-col,-obs)
ggplot(df_l,aes(x=key,y=value,colour=col,group=obs)) +
geom_line()
ggplot(df_l,aes(x=key,y=value,group=obs)) +
geom_line()
m3 %<>%
#因子排序
mutate(County=factor(County,levels = unique(County))) %>%
mutate(IncidenceFactor=cut(Incidence,
breaks = c(-1,0,0.1,0.5,1.0,5.0,10.0,
max(m3$Incidence,na.rm = T)),
labels = c("0","0-0.1","0.1-0.5","0.5-1.0","1.0-5.0","5.0-10.0",
">10.0")))
library(ggplot2)
ggplot(m3,aes(x = factor(Year), y = County)) +
geom_tile(aes(fill = Incidence),colour = "white",size = 0.25) +
scale_y_discrete(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0),
breaks = c("1960","1970","1980","1990","2000","2010")) +
coord_fixed() +
theme_grey(base_size = 8) +
theme(axis.text = element_text(face = "bold"),
axis.ticks = element_line(size = 0.4),
plot.background = element_blank(),
panel.border = element_blank())
textcol <- "black"
ggplot(m3,aes(x = factor(Year), y = County, fill = IncidenceFactor)) +
geom_tile(colour = "white", size = 0.25) +
labs(x = "", y= "", title = "Incidence of JE in China") +
scale_y_discrete(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0),
breaks = c("1960","1970","1980","1990","2000","2010")) +
scale_fill_manual(values = c("#d53e4f","#f46d43","#fdae61","#fee086",
"#e6f598","#ddf1da","#abdda4"),
na.value = "grey90") +
theme_grey(base_size = 8) +
theme(legend.position = "right",legend.direction = 'vertical',
legend.title = element_blank(),
legend.text = element_text(colour = textcol,
size = 6,
face = 'bold'),
legend.key.height = grid::unit(0.8,"cm"),
legend.key.width = grid::unit(0.2,"cm"),
axis.text.x = element_text(size =8,colour = textcol),
axis.text.y = element_text(vjust = 0.2,colour = textcol),
axis.ticks = element_line(size = 0.4),
plot.background = element_blank(),
panel.border = element_blank(),
plot.title = element_text(colour = textcol,
hjust = 0.5,
size = 14,
face = 'bold'))
ggplot(m3,aes(x = factor(Year), y = County, fill = factor(m3$IncidenceFactor,levels = rev(levels(m3$IncidenceFactor))))) +
geom_tile(colour = "white", size = 0.25) +
labs(x = "", y= "", title = "Incidence of JE in Gansu") +
scale_y_discrete(expand = c(0,0)) +
scale_x_discrete(expand = c(0,0),
breaks = c("1960","1970","1980","1990","2000","2010")) +
scale_fill_manual(values = c("#d53e4f","#f46d43","#fdae61","#fee086",
"#e6f598","#ddf1da","#abdda4"),
na.value = "grey90") +
theme_grey(base_size = 8) +
theme(legend.position = "bottom",legend.direction = 'horizontal',
legend.title = element_blank(),
legend.text = element_text(colour = textcol,
size = 8,
face = 'bold'),
legend.key.height = grid::unit(0.1,"cm"),
legend.key.width = grid::unit(0.5,"cm"),
axis.text.x = element_text(size =8,colour = textcol))+
#in one row
guides(colour = guide_legend(nrow=1),fill=guide_legend(nrow=1,reverse=TRUE))
library(tidyverse)
obs<-c('a','b','c','d')
before<-c(2,4,6,8)
after<-c(4,2,8,4)
df<-cbind(before, after)
row.names(df)=obs
df <- as.data.frame(df)
df$col <- factor(df$after - df$before > 0,labels=c("descending","ascending"))
df$obs <- obs
df_l <- gather(df,key,value,-col,-obs)
ggplot(df_l,aes(x=key,y=value,colour=col,group=obs)) +
geom_line()
ggplot(df_l,aes(x=key,y=value,group=obs)) +
geom_line()
评论
发表评论