美文网首页
coord_polar() 风玫瑰图 2020-04-06

coord_polar() 风玫瑰图 2020-04-06

作者: 深情的庆 | 来源:发表于2020-04-06 16:47 被阅读0次

Sys.setlocale(,"CHS")  # 控制让电脑可以读取中文字符

library(ggplot2)

# 风玫瑰图

ggplot(mtcars, aes(x = factor(cyl))) +

  geom_bar()

ggplot(mtcars, aes(x = factor(cyl))) +

  geom_bar()+

  coord_polar()

# 加上宽度 颜色控制

ggplot(mtcars, aes(x = factor(cyl))) +

  geom_bar(width = 1, colour = "black")

ggplot(mtcars, aes(x = factor(cyl))) +

  geom_bar(width = 1, colour = "black")+

  coord_polar()

# 饼图

ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) +

  geom_bar(width = 1)

ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) +

  geom_bar(width = 1)+

  coord_polar(theta = "y")

# 疫情人数数据

x <- c("1","34","52","102","126","154")

province <- c("广东省", "河南省", "浙江省", "湖南省", "安徽省", "江西省")

sure <- c("1352","1272","1215","1018","990","935")

data <- data.frame(x,province,sure)

names(data) <- c("x","省","省确诊")

str(data)

data$省确诊 <- as.numeric(data$省确诊)

# 疫情柱状图

ggplot(data)+

  geom_bar(stat="identity",aes(y=省确诊,x=reorder(省, 省确诊),fill=省确诊))

#这里要reorder一下,让省按照省确诊来排列

# 渐变色填充 疫情风玫瑰图

col <- ggplot(data, aes(y=省确诊,x=reorder(省, 省确诊),fill=省确诊))+

  geom_bar(stat="identity")+

  coord_polar()

col

#粉白渐变色

col2 <- col+

  scale_fill_gradient(low='white',high='red')+ #渐变色填充,由白到红

  theme_bw()

col2

#无边框 图例

  col3 <- col2+theme(axis.text.y = element_blank(),

        axis.text.x = element_text(colour = 'black', face = 'bold',size = 9),

        panel.border = element_blank())+

  xlab(label = '')+ylab(label = '')+

  guides(fill=FALSE)

col3

# 文字位置改变

# install.packages("ggrepel")

# library(ggrepel)

col3+geom_text_repel(aes(label = 省, y = 省确诊,x=省),size=3)+

  theme(axis.text.y = element_blank(),

        axis.text.x = element_text(colour = 'white', face = 'bold',size = 2),

        panel.border = element_blank())

//摘自https://mp.weixin.qq.com/s/fJcfotv9umV9pk0iTi246w

相关文章

网友评论

      本文标题:coord_polar() 风玫瑰图 2020-04-06

      本文链接:https://www.haomeiwen.com/subject/niubphtx.html