绘图导航
  • 基础绘图
  • 高级绘图
  • 微生物
  • 转录组

R语言|绘制棒棒糖图

 二维码 3

R语言|绘制棒棒糖图

library(ggplot2)

# 创建数据框

city<-c("广东","江苏","山东","浙江","河南","四川","福建","湖北","湖南","上海","安徽","河北","北京","陕西","江西","辽宁","重庆","云南","广西","贵州","山西","内蒙古","天津","新疆","黑龙江","吉林","甘肃","海南","宁夏","青海","西藏")

gdp<-c(110761,102719,73129,64613,54997,48599,43904,43443,41781,38701,38681,36207,36103,26182,25692,25115,25003,24522,22157,17827,17652,17360,14084,13798,13699,12311,9017,5532,3921,3006,1903)

data1<-data.frame(city,gdp)

# 绘图

ggplot(data1, aes(x=city, y=gdp)) +

  geom_segment( aes(x=city, xend=city, y=1, yend=gdp), color="grey") +

  geom_point( color="skyblue", size=3) + #散点

  theme_light() +

  theme(axis.text.x = element_text(angle = 45, hjust = 0.8, vjust = 0.9),#调整x轴标签角度

    panel.grid.major.x = element_blank(),

    panel.border = element_blank(),

    axis.ticks.x = element_blank()

  ) +

  xlab("city") +

  ylab("GDP")



#改变基准线:在geom_segment()函数中更改y的值

ggplot(data1, aes(x=city, y=gdp)) +

  geom_segment( aes(x=city, xend=city, y=30000, yend=gdp), color="grey") + #更改基准线

  geom_point( color="skyblue", size=3) +

  theme_light() +

  theme(axis.text.x = element_text(angle = 45, hjust = 0.8, vjust = 0.9),#调整x轴标签角度

        panel.grid.major.x = element_blank(),

        panel.border = element_blank(),

        axis.ticks.x = element_blank()

  ) +

  xlab("city") +

  ylab("GDP")


#####水平转置坐标轴

ggplot(data1, aes(x=city, y=gdp)) +

  geom_segment( aes(x=city, xend=city, y=0, yend=gdp), color="orange") + #更改基准线

  geom_point( color="skyblue", size=3) +

  theme_light() +

  coord_flip() +

  theme(axis.text.x = element_text(angle = 45, hjust = 0.8, vjust = 0.9),#调整x轴标签角度

        panel.grid.major.x = element_blank(),

        panel.border = element_blank(),

        axis.ticks.x = element_blank()

  ) +

  xlab("city") +

  ylab("GDP")


####调整排序

data1$city<-factor(data1$city) #转换成因子

data1$city<-reorder(data1$city,data1$gdp) #这是升序,降序加上负号即可

ggplot(data1, aes(x=city, y=gdp)) +

  geom_segment( aes(x=city, xend=city, y=0, yend=gdp), color="orange") + #更改基准线

  geom_point( color="skyblue", size=3) +

  theme_light() +

  coord_flip() +

  theme(axis.text.x = element_text(angle = 45, hjust = 0.8, vjust = 0.9),#调整x轴标签角度

        panel.grid.major.x = element_blank(),

        panel.border = element_blank(),

        axis.ticks.x = element_blank()

  ) +

  xlab("city") +

  ylab("GDP")



“作图帮”微信公众号同步更新

作图帮二维码.jpg


可添加下方微信进入【生信作图交流群】,群内免费分享绘图代码与示例数据

公司二维码.png


推荐使用谷歌浏览器点击下载